2

How can I access individual cells in a csv document to parse and edit them? Also is there a way I can convert the csv document into a two-dimensional array?

4

2 回答 2

2

您将要使用为 python 制作的csv 包。您可能想尝试这样的事情(假设您的文件是有规律的结构并且没有标题):

data = []
for row in csv.reader(open('you_file.csv', 'rb'), delimiter=',')
    data.append(row)

尝试一下并查看csv 包文档以了解更多信息。

于 2012-12-05T03:06:23.017 回答
1

这是为您提供的解决方案。

import csv

twoDimArray = [] 

with open('input.csv', 'rb') as csvfile: 
    reader = csv.reader(csvfile, delimiter=';', quotechar='|') 
    for row in reader: 
        twoDomArray.append(row) 

#DO STUFF WITH DATA 

with open('output.csv', 'wb') as csvfile: 
    writer = csv.writer(csvfile, delimiter=' ', 
                            quotechar='|', quoting=csv.QUOTE_MINIMAL) 
    for row in twoDinArray: 
        writer.writerow(row) 

另请阅读文档

祝你好运:)

于 2012-12-05T03:10:58.803 回答