开发者

to merge two columns in a csv file [closed]

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜