Python Forum
Reading and writing to text file has format change
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading and writing to text file has format change
#1
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

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
Reply
#2
You are adding a line feed ('\n') to the write buffer here:
for row in xdata:
f.write('%s\n' % row)
so you are not writing out what you read in.
Reply
#3
Hi

I remove \n in "f.write('%s' % row)" but it doesnt work. It keeps running and I have to close the python shell to shut it off. I check the test.txt and the data is continuous. ie ['X', 'X' , ...] [ 'X1', 'X2' , ...]. I think I should have \n so that I can see the data in each row in test.txt. Any idea to write the data in each row in test.txt and read back exactly what I write to it? tks



f= open('data.txt', 'r')

xdata=[]

for row in f:
    row = row.rstrip()
    row = row.split('\t')
    xdata.append(row)

f.close()

f=open('test.txt', 'w')

for row in xdata:
    f.write('%s' % row)

f.close()

f= open('test.txt', 'r')

xdata1=[]

for row in f:
    row = row.rstrip()
    row = row.split('\t')
    xdata1.append(row)

f.close()

print(xdata1[0:3])
Reply
#4
I added some print statements to show what's happening in the code, this before a recommended way to write
f = open('data.txt', 'r')

xdata = []

for n, row in enumerate(f):
    print(f'row after line 5: n: {n}, {row}')
    row = row.rstrip()
    print(f'row after line 7: n: {n}, {row}')
    row = row.split('\t')
    print(f'row after line 9: n: {n}, {row}')
    xdata.append(row)
    print(f'xdata end of iteration {n}: {xdata}')
f.close()

f = open('test.txt', 'w')

for row in xdata:
    f.write('%s' % row)

f.close()

f = open('test.txt', 'r')

xdata1 = []

for row in f:
    row = row.rstrip()
    row = row.split('\t')
    xdata1.append(row)

f.close()

print(xdata1[0:3])
which produces:
Output:
row after line 5: n: 0, [['DATE', 'SEC_ID', 'CTRY_ID', 'MKT_CAP'], row after line 7: n: 0, [['DATE', 'SEC_ID', 'CTRY_ID', 'MKT_CAP'], row after line 9: n: 0, ["[['DATE', 'SEC_ID', 'CTRY_ID', 'MKT_CAP'],"] xdata end of iteration 0: [["[['DATE', 'SEC_ID', 'CTRY_ID', 'MKT_CAP'],"]] row after line 5: n: 1, ['9/30/2006', '67', 'US', '12.69'], row after line 7: n: 1, ['9/30/2006', '67', 'US', '12.69'], row after line 9: n: 1, ["['9/30/2006', '67', 'US', '12.69'],"] xdata end of iteration 1: [["[['DATE', 'SEC_ID', 'CTRY_ID', 'MKT_CAP'],"], ["['9/30/2006', '67', 'US', '12.69'],"]] row after line 5: n: 2, ['9/30/2006', '44', 'US', '11.93']] row after line 7: n: 2, ['9/30/2006', '44', 'US', '11.93']] row after line 9: n: 2, ["['9/30/2006', '44', 'US', '11.93']]"] xdata end of iteration 2: [["[['DATE', 'SEC_ID', 'CTRY_ID', 'MKT_CAP'],"], ["['9/30/2006', '67', 'US', '12.69'],"], ["['9/30/2006', '44', 'US', '11.93']]"]] [['["[[\'DATE\', \'SEC_ID\', \'CTRY_ID\', \'MKT_CAP\'],"]["[\'9/30/2006\', \'67\', \'US\', \'12.69\'],"]["[\'9/30/2006\', \'44\', \'US\', \'11.93\']]"]']]
However, I would write this way (without regard to errors at this point):
with open('data.txt') as f, open('test.txt', 'w') as fo:
    for row in f:
        row = row.strip().split('\t')
        fo.write(f'{row}\n')
        fo.write(str(row))

# Read back
with open('test.txt') as f:
    for row in f:
        print(row)
which produces:
Output:
["[['DATE', 'SEC_ID', 'CTRY_ID', 'MKT_CAP'],"] ["[['DATE', 'SEC_ID', 'CTRY_ID', 'MKT_CAP'],"]["['9/30/2006', '67', 'US', '12.69'],"] ["['9/30/2006', '67', 'US', '12.69'],"]["['9/30/2006', '44', 'US', '11.93']]"] ["['9/30/2006', '44', 'US', '11.93']]"]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad problems with reading csv file. MassiJames 3 608 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Reading a file name fron a folder on my desktop Fiona 4 889 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  logging: change log file permission with RotatingFileHandler erg 0 999 Aug-09-2023, 01:24 PM
Last Post: erg
  How can I change the uuid name of a file to his original file? MaddoxMB 2 912 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,085 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 951 Feb-15-2023, 05:34 PM
Last Post: zsousa
  Reading a file JonWayn 3 1,089 Dec-30-2022, 10:18 AM
Last Post: ibreeden
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,105 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Reading Specific Rows In a CSV File finndude 3 968 Dec-13-2022, 03:19 PM
Last Post: finndude
  Excel file reading problem max70990 1 889 Dec-11-2022, 07:00 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020