我正在使用一个大型数据集 (OMNI),我正在寻找解析数据并将每行数据放入一个列表的数组的方法。我对 Python 还很陌生,所以我边走边学。
这就是我所拥有的:
import Tkinter, tkFileDialog
import csv
#Choose the file that you want to read from
root = Tkinter.Tk()
root.withdraw()
file_path = tkFileDialog.askopenfilename()
current_file = open(file_path , "r")
#OMNI_2001 = {}
reader = csv.reader(current_file, delimiter= ' ')
output_file = open('newdata.txt','w')
out = csv.writer(output_file)
for row in reader:
out.writerow(row)
print row
#print row[0::1]
我读入的一行数据如下所示:
2001 182 0 0 60 60 7 2 71 -695 320 0.22 -173 6.07 5.23 0.46 -2.00 0.69 -1.93 0.38 2.09 331.0 -329.5 24.5 19.8 8.66 101479. 1.90 0.64 2.25 8.0 6.67 29.65 3.55 12.73 -1.78 -0.70 288 -142 146 -3 -22 20 19 0.99
但在我输出新数据后,如下所示:
2001,182,,0,,0,60,60,,,7,,,2,,71,,,-695,,,,320,,0.22,,,-173,,,,6.07,,,,5.23,,,,0.46,,,-2.00,,,,0.69,,,-1.93,,,,0.38,,,,2.09,,,331.0,,-329.5,,,,24.5,,,,19.8,,,8.66,,101479.,,1.90,,,0.64,,,2.25,,,8.0,,,,6.67,,,29.65,,,,3.55,,,12.73,,,-1.78,,,-0.70,,,288,,-142,,,146,,,,-3,,,-22,,,,20,,,,19,,,0.99
我在做什么导致这么多额外的逗号?另外我将如何删除不需要的条目?