开发者

to merge two columns in a csv file [closed]

开发者 https://www.devze.com 2023-01-05 22:35 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. 开发者_JAVA技巧 Closed 12 years ago.

merge two columns in a csv file


Here is an example, dont know your delimiter. if you want to write it to the same file you have to buffer the whole file first, modify the rows, then write it back to the same file.

 import csv
 for row in csv.reader(open('test.txt'),delimiter="\t"):
     print row[0]+row[1]


    fin = open('file.csv', 'r+')
    fout = open('NEW.csv','w')
    for line in fin.xreadlines():
        new = line.replace(',', ' ', 1)
        fout.write (new)
    fin.close()
    fout.close()

Assuming that "file.csv" is the input, and "NEW.csv" is the output. Also the first comma is getting replaced by space. You can alter that by modifying

new = line.replace(',', ' ', 1)

and replacing the second argument with anything you want

0

精彩评论

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