I would like to take a file that is comma delimited, read it, and in the first column, change every 7 into a 4. I think there are a few ways to skin this cat, looking for the best way, Any ideas?

A brief view of what the file looks like:
1,10002,0,11:18,11:18,0,1,444,1
4,1.5
5,1.5
6,0,444,444,10002
7,0,1.5
1,10003,0,11:34,11:34,0,1,444,1
4,1.5
5,1.5
6,0,444,444,10003
7,0,1.5
1,10004,0,11:39,11:39,0,1,444,1
4,7.25
5,7.25
6,0,444,444,10004
7,0,7.25

You are right, there are many ways.

'''
let's assume your data file "7to4.txt" would look like this:
1,10002,0,11:18,11:18,0,1,444,1
4,1.5
5,1.5
6,0,444,444,10002
7,0,1.5
1,10003,0,11:34,11:34,0,1,444,1
4,1.5
5,1.5
6,0,444,444,10003
7,0,1.5
1,10004,0,11:39,11:39,0,1,444,1
4,7.25
5,7.25
6,0,444,444,10004
7,0,7.25
'''

new_string = ""
# each line is string so you can use string functions
for line in file("7to4.txt"):
    if line.startswith('7'):
        # replace first 7 with 4
        line = line.replace('7', '4', 1)
    new_string += line

print new_string

That worked great!!!!!

Just one more question. The file I am picking up has yesterdays date on it.("CF090909.csv")

The CF is always the same, but the date is always yesterday. How can I use datetime to assign the filename to look for as a variable?

this is where i'm at now:

from shutil import copy
import os
import datetime
from csv import reader
from csv import writer
copy('c:\\outfiles\\CF092108.prn','C:\\outfiles\\newcf')

f = open("c:\\outfiles\\newcf\\CF092108.prn", "rb")
f.next()
for row in reader(f):
print row

#
new_string = ""
#
# each line is string so you can use string functions
#
for line in file("c:\\outfiles\\newcf\\CF092108.prn"):
#
if line.startswith('7'):
#
# replace first 7 with 4
#
line = line.replace('7', '4', 1)
#
new_string += line
#

#

csv_writer=csv.writer('c:\\outfiles\\newcf\\CF092108.prn')
csv_writer.writerows('new_string')


getting an error at the writer line and i still don't know how to add a variable for yesterdays date in the file name. Any thoughts?

Something like this can do:

import datetime

today = datetime.date.today()
yesterday = (today + datetime.timedelta(days=-1))

filename = "CF" + yesterday.strftime("%m%d%y") + ".csv"
print filename  # for instance --> CF092108.csv

Please use code tags with your code to preserve the indentations. Otherwise the code is very difficult to read and not too many folks will help.

[code=python]
your Python code here

[/code]

sorry about the code tags. so, if i declare the CF(date) as filename, how do i pass that to the location of the file? and if anyone could advise me on the writer at the bottom, it is saying i am not defined, but, i thought i defined it in the first row.

Thanks again, this forum is a huge help

It's best to combine your folder and file names with os.path.join(), then write your file out to the full path name:

import os

folder = 'c:\\outfiles\\newcf'
filename = 'CF092108.csv'
fullpath = os.path.join(folder, filename)
print fullpath  # c:\outfiles\newcf\CF092108.csv

# now write your string out
# it contains all the .csv data
fout = open(fullpath, 'w')
fout.write(new_string)
fout.close()

You can also use that path name for your input.

This article has been dead for over six months. Start a new discussion instead.