Python CSV Module read and write simultaneously
I have two .csv files I am looking up data in one (file a) and matching it to the other (file b) once I find the appropriate row in b I want to write to a specific cell in the appropriate row. Additionally I need to iterate over this so I will likely be writing to each row in file b potentially several times.
can I write to a csv file and then read it over and over again?
def match(name, group, cnum):
for data in masterfile_list:
if (name in data[0]):
if (group in data[4]):
if (cnum == "112"):
data[7] = cnum
elif (cnum == "111"):
data[8] = cnum
elif (cnum == "110"):
data[9] = cnum
elif (cnum == "109"):
data[10] = cnum
elif (cnum == "108"):
data[11] = cnum
elif (cnum == "107"):
data[12] = cnum
elif (cnum == "106"):
data[13] = cnum
elif (cnum == "105"):
data[14] = cnum
elif (cnum == "104"):
data[15] = cnum
elif (cnum == "103"):
data[16] = cnum
elif (cnum == "102"):
data[17] = cnum
elif (cnum == "101"):
data[18]开发者_开发知识库 = cnum
I would ideally write/replace the line that matches.
If file b is not extremely large I would suggest using readlines()
to get a list of all lines and then iterate over the list and change lines as needed. This will be quite a bit easier than seeking to different positions in the file and replacing lines.
Also, you can significantly reduce the code in the body of your function, I would probably do something like this:
def match(name, group, cnum):
lookup = dict(zip(map(str, range(112, 100, -1)), range(7, 19)))
for data in masterfile_list:
if name in data[0] and group in data[4] and cnum in lookup:
data[lookup[cnum]] = cnum
I'm not sure from your code fragment how you are opening/reading/writing the files in question.
To do what you describe you want, and if the files are not too large, I would read the relevant source files into memory, alter the structures you need to in memory, then write out the result to a file.
pseudocode
file_a=open('file_a','r')
file_b_things=open('file_b','r').readlines()
new_things_file=open('new_things','w')
new_things=[]
for thing in file_a:
if thing in file_b_things:
new_thing=do_something_with(thing)
new_things.append(new_thing)
for new_thing in new_things:
new_things_file.write(new_thing)
It is generally not easy to replace lines in a file. Normally, you would have to rewrite the file (in a secure way), if you want to make changes.
Except if you exactly know that the line size will not change - then you could ftell()
before reading a line and seek()
afterwards. But even that is not quite safe, because the readline()
stuff in python does some buffering, i.e. the file pointer is nearer to the end than it should be.
精彩评论