只要我不断添加变量,即 z=[] 和 z.append(line[2]) 等,这对我来说效果很好……也许我误解了这些问题?
import csv
x,y,z = [],[],[]
csv_reader = csv.reader(open('Data.csv'))
for line in csv_reader:
x.append(line[0])
y.append(line[1])
z.append(line[2])
如果您从电子表格复制并粘贴到文本文件,您可以打开('Data.txt')并用 \t 分割每一行,如果这是列之间的分隔符。
xx,yy,zz = [],[],[]
fromtextfile = open('Data.txt')
#(append each list) for
#item in the line, split by tabs, into a list for line in the file
[(xx.append(item[0]),yy.append(item[1]),zz.append(item[2])) \
for item in [line[:-1].split('\t') for line in fromtextfile]]
#or
xxx,yyy,zzz = [],[],[]
fromtextfile = open('Data.txt')
temp = []
for line in fromtextfile:
temp.append(line[:-1])
for item in temp:
templist = item.split('\t')
xxx.append(templist[0])
yyy.append(templist[1])
zzz.append(templist[2])
fromtextfile.close()