python: updating file by finding unique values between two files, and then appending further data to bottom -
i haven't got idea python. have file, substance.txt, list of 4k substances. have log file, log.txt, contains updates these substances @ moment, manually reflecting in substance.txt. log has format + tab @ start if new concept or -tab @ start if concept should removed substance.txt
using python, tying go through , first, copy in substance.txt not in log new file. then, trying go through logfile , append has '+ tab' bottom of new file. give me existing substance.txt content not affected + new terms log.txt , have removed concepts flagged in log.txt removal.
this code:
import re import fileinput # write concepts not not in log open("log.txt", 'r') f, open("substance.txt", "r") oldfile, open('new_substance.txt', 'w') newfile: withconceptsremoved = [x x in oldfile if x not in f] newfile.write(withconceptsremoved) # new file has comments neither positive or negative in log. if copy positive ones, we've removed negatives # write new additions bottom of new file line in f: if '+\t' in line: addedconcept = line.replace('+\t','1\t') newfile.write(addedconcept)
this error:
line 8, in newfile.write(withconceptsremoved) typeerror: expected character buffer object
if remove
withconceptsremoved = [x x in oldfile if x not in newfile.write(withconceptsremoved)
it works. looked @ typeerror: expected character buffer object - while trying save integer textfile didn't understand it
i tried code , found several issues prevented wanted.
- the list "withconceptsremoved" needs converted string stated in question comments
- you reading "f" 2 times, need seek file start each time (or try read once)
- the "if x not in f" not work cause need use list instead of "f"
- you not taking "-\t" , "+\t" account when doing "if x not in f"
i fixed issues , seems working fine me.
here's updated code came with:
import re import fileinput # write concepts not not in log open("log.txt", 'r') f, open("substance.txt", "r") oldfile, open('new_substance.txt', 'w') newfile: # read f once , convert list loglist = list(f) # remove exists in f either +\t or -\t withconceptsremoved = [x x in oldfile if ('-\t'+x not in loglist , '+\t'+x not in loglist)] # convert string , write file withconceptsremoved = "".join(withconceptsremoved) newfile.write(withconceptsremoved) # new file has comments neither positive or negative in log. if copy positive ones, we've removed negatives # write new additions bottom of new file line in loglist: if '+\t' in line: addedconcept = line.replace('+\t','1\t') newfile.write(addedconcept)
Comments
Post a Comment