开发者

Best method for appending new column in tab delimited csv file

开发者 https://www.devze.com 2023-02-24 22:45 出处:网络
How can I read and write at the same time in csv file. def read_txt_file(txt_filename): def get_all_sheet_values(filename):

How can I read and write at the same time in csv file.

def read_txt_file(txt_filename):

def get_all_sheet_values(filename):
    lines = []
    myFile= open( filename, "rU" )
    for aRow in myFile:
        val =  aRow.split('\t')
        val = map(lambda s: s.strip(), val)
        if len(val) 开发者_如何学Go> 1 :
            print val
            lines.append(val)
    myFile.close()
    return lines

files_dict = get_all_sheet_values(txt_filename)
return files_dict

So basically this code works file.

my problem is writing to the same csv file. example,

10 10 10 10 10 \n
11 11 11 11 11  \n
22 22 22 22 22  \n

so after iterating each row I have to add status to it.

10 10 10 10 10 correct time1 \n
11 11 11 11 11 wrong   time2  \n
22 22 22 22 22 correct time3  \n

thanks.


Files don't work that way. Write to a new file, and rename it after.


You can't. When you add the column you are making each line longer. If you were to write it out before going to the next line you would overwrite a portion of it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号