I haven't got any idea about python. But I do have a file, substance.txt, which is a list of about 4k substances. I have a log file, log.txt, which contains updates to these substances that at the moment, I am manually reflecting in substance.txt. The log has the format + tab at the start if it is a new concept or -tab at the start if it is a concept which should be removed from substance.txt
Using python, I am tying to go through and first, copy everything in the substance.txt which is not in the log to a new file. Then, I am trying to go through the logfile and append anything which has '+ tab' to the bottom of the new file. That will give me all the existing substance.txt content which is not affected + any new terms from log.txt and will have removed any concepts which are flagged in log.txt for removal.
This is my code:
import re
import fileinput
# write concepts which are not not in log
with open("log.txt", 'r') as f, open("substance.txt", "r") as oldfile,
open('new_substance.txt', 'w') as newfile:
withconceptsremoved = [x for x in oldfile if x not in f]
newfile.write(withconceptsremoved)
# so the new file only has comments which are neither positive or negative in log. If we now copy positive ones, we've removed the negatives
# write new additions to bottom of new file
for line in f:
if '+\t' in line:
addedconcept = line.replace('+\t','1\t')
newfile.write(addedconcept)
this is my error:
line 8, in newfile.write(withconceptsremoved) TypeError: expected a character buffer object
If I remove the
withconceptsremoved = [x for x in oldfile if x not in
newfile.write(withconceptsremoved)
it works. I looked at this TypeError: expected a character buffer object - while trying to save integer to textfile but didn't understand it