Dec-14-2017, 02:57 AM
Dear Sir,
I have been puzzled by the change of format between reading and writing the same content to a file. I have written my program base on the first format shown below
['9/30/2006', '67', 'US', '12.69'],
['9/30/2006', '44', 'US', '11.93']]
However when I write the above, xdata to a file and read it back. I get the following
["['9/30/2006', '67', 'US', '12.69']"],
["['9/30/2006', '44', 'US', '11.93']"]]
Hence, my program cannot work. Do I need to have some settings to do a proper write and proper read back to remove the additional [" ... "] in each line or do I need to remove them manually? If so, how?
Thank you for your advices
L
I have been puzzled by the change of format between reading and writing the same content to a file. I have written my program base on the first format shown below
f= open('data.txt', 'r') xdata=[] for row in f: row = row.rstrip() row = row.split('\t') xdata.append(row) f.close() print(xdata[0:3])[['DATE', 'SEC_ID', 'CTRY_ID', 'MKT_CAP'],
['9/30/2006', '67', 'US', '12.69'],
['9/30/2006', '44', 'US', '11.93']]
However when I write the above, xdata to a file and read it back. I get the following
f=open('test.txt', 'w') for row in xdata: f.write('%s\n' % row) f.close() f= open('test.txt', 'r') xdata1=[] for row in f: row = row.rstrip('\n') row = row.split('\t') xdata1.append(row) f.close() print(xdata1[0:3])[["['DATE', 'SEC_ID', 'CTRY_ID', 'MKT_CAP']"],
["['9/30/2006', '67', 'US', '12.69']"],
["['9/30/2006', '44', 'US', '11.93']"]]
Hence, my program cannot work. Do I need to have some settings to do a proper write and proper read back to remove the additional [" ... "] in each line or do I need to remove them manually? If so, how?
Thank you for your advices
L