I have the following code that extracts needed data from a xml file. I can save it to terminal, and open it with no problem. I am in trouble inserting column names to the txt file, however. I have been searching the answer but found no right solution. Would anyone help me here? Thank you!!
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
orig_stdout = sys.stdout
sys.stdout = f
for program in root.findall('program'):
programID = program.find('programID')
season = program.find('season')
print programID, season
sys.stdout = orig_stdout
f.close()
解决方案
The way to write data to a file in Python is to call .write(content) on the file object, not to redirect stdout to it.
Instead of all your lines messing with sys.stdout, try this:</