Segment a txt file column to lists
我有多个列的文本文件,用逗号分隔。
我试着读它,并把每一列放在它自己的单独列表中,但我似乎做不到。
到目前为止我所做的:
1 2 3 4 5 6 7 | with open(file, 'r') as file_test: file_lines = file_test.readlines() file_strip = [line.strip(" ") for line in file_lines] #I've split big list into separate lists within `file_strip` file_columns= [file_strip [i:i + 1] for i in range(0, len(file_strip ), 1)][2:] |
现在我的数据如下:
1 | [['22AUG18 000000, 22AUG18 000149, 5.722, UOS2'], ['22JUL18 012703, 22JUL18 013810, 52.2811, UOS2']] |
我也不知道如何去掉每个列表开头和结尾的
我希望每个列表中的第一个元素在
为什么不使用
1 2 3 4 5 6 | import csv with open(file, 'r') as file_test: csv_test = csv.reader(file_test) for row in csv_test: print(row) |
将打印
1 2 | ['22AUG18 000000', '22AUG18 000149', '5.722', 'UOS2'] ['22JUL18 012703', '22JUL18 013810', '52.2811', 'UOS2'] |
如果要在列表中分离它,可以将其压缩():
1 2 3 | with open(file, 'r') as file_test: csv_test = csv.reader(file_test) list1, list2, list3, list4 = zip(*csv_test) |