[Tutor] Saving file changes to output
Daniel D. Laskey, CPA
dlaskey@laskeycpa.com
Wed, 2 Aug 2000 12:54:46 -0400
I've been struggling for the last couple of days on this problem.
So it is not as if I am asking the group to do my homework for
me. Again, I'm a newbie. I've been studing hard. Maybe I'm just
learning impaired. Please be kind.
The "data_file" looks like this:
33.04,123196,12,421020,RVN# 179-Ernest Lonard,12528.7
33.04,123196,12,421020,RVN# 196-Hector Gale,2599.7
33.04,123196,12,421230,RVN# 249-Richard George,2300
33.04,123196,12,420000,Total Sales Journal,-17428.4
import sys
import string
in_file = open("data_file","r")
out_file = open("junk2.txt","w")
for line in in_file.readlines():
x = string.split(line,',')
# read each line of file and split at each ","
f1 = x[4]
# pull out the fifth position 0,1,2,3,4 of each line
f2 = '"' + f1 + '"'
# put double quotes " " around this data
f3 = x[0]+","+x[1]+","+x[2]+","+x[3]+","+f2+","+x[5]
# print f3
# rebuild the file using the new position [4] with double quotes
out_file.write(line,f3)
out_file.close()
in_file.close()
#Traceback (innermost last):
# File "today.py", line 15, in ?
# out_file.write(line,f3)
#TypeError: read-only buffer, tuple
My objective is to write the changes I made to the data to my out_file. When I
put a print statement after the f3 the file looks fine. I just can't get it to print
to the junk2.txt file properly.
Please help ole kind ones.
Dan
-----------------------------------------------------------------------------------------------
| Daniel D. Laskey, CPA--------------------dlaskey@laskeycpa.com
| Daniel D. Laskey Company, P.C.--------231-723-8305 / Fax 231-723-6097
| Certified Public Accountants
| 507 Water Street
| Manistee, MI 49660
-----------------------------------------------------------------------------------------------