[Tutor] Loading and writing files
Tim Condit
timc@ans.net
Mon, 31 Jul 2000 16:36:50 -0400 (EDT)
Hi,
I'll take a crack at it..
These two lines (when used together) are your first problem, I think.
in_file = raw_input(".CSV file name? ")
in_file = open(x,"r")
You're trying to open a file with a variable x that has no associated
filename.
Next, the file you'll write to should be opened with 'a', instead of 'w',
as this is repeatedly writing each line, over the line that came before
it. That's not what you want, I'm guessing.
I'd remove a couple lines too:
import string
import sys
print "Enter the name of the .CSV file to convert:"
x = raw_input(".CSV file name? ")
in_file = open(x,"r")
out_file = open("junk2.txt","a")
for line in in_file.readlines():
print string.strip(line)
out_file.write(line)
out_file.close()
in_file.close()
--
Take a shot at the third part, and write again if you don't figure it out.
Maybe string.split the line on the commas, then use slice notation on the
string you want to modify, then paste it all back together.
something like:
for line in in_file.readlines():
f = string.split(line, ',')
f[4] = add quotes
f = f[:4] + f[4] + f[4:]
or something like that. But I'm learning myself, so there are probably
better ways to do it. But it's always a good idea to try a few things for
yourself, then ask questions when you're stuck.
Good luck,
Tim Condit
UUNet Network Quality Services
734-214-7548
tcondit@uu.net
On Mon, 31 Jul 2000, Daniel D. Laskey, CPA wrote:
> #-----------Objective---------------------------------------
> #
> # 1. Open a file that is comma deiminated called test.csv
> # and then print the file to the screen.
> # 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
> #
> # 2. Write the file to a file called junk2.txt and be able
> # to "cat" or "type" the file after the program terminates.
> #
> # 3. Read the data in the 5th position of the file and put " "s
> # around the data, so that the ending file lools 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
> #
> #-----------My Code----------------------------------------------
>
> import string
> import sys
>
> print "Enter the name of the .CSV file to convert:"
> in_file = raw_input(".CSV file name? ")
> in_file = open(x,"r")
> text = in_file.read()
> print text
> out_file = open("junk2.txt","w")
> for line in in_file.readlines():
> out_file.write()
> out_file.close()
> in_file.close()
>
> #-------------End of Code--------------------------------------------
> #
> # Thanks in advance for your help.
> # 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
> #--------------------------------------------------------------------
>
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://www.python.org/mailman/listinfo/tutor
>