[英]How to extract words from CSV file and store to list in Python
outputDatafile = open('D:\\Files\\input.csv', 'r')
outputReader = csv.reader(outputDatafile, delimiter=',', quoting=csv.QUOTE_NONE)
print "OutputReader data type:",type(outputReader)
inputData= []
//i want to extract all values in csv to array
for row in outputReader:
inputData.append(row)
我有一个CSV文件,如下所示:
名称1,值11,值12,值13
name2,value21,value22,value23,value24
名称3
我想提取并存储所有要列出的项目,例如:
list[0]=name1
list[1]=value11
list[2]=value12
.
.
.
.
list[5]=name2
list[6]=value21
使用extend()
而不是append()
。 后者将每个列表添加为外部列表中的元素,前者将列表连接在一起。 您也可以使用+=
。
with open(r'D:\Files\input.csv') as f:
input_data = []
for row in csv.reader(f, delimiter=',', quoting=csv.QUOTE_NONE):
input_data += row
.csv
可以看作是带有分隔符','的普通txt文档。因此,您唯一需要做的就是将字符串拆分为:
outputDatafile = open('D:\\Files\\input.csv', 'r')
inputData= []
for rows in outputDatafile:
row = rows.rstrip().spilt(',') #row is a list of strings splited by ',' of rows.
#and row[0] is name1 and so on
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.