[英]Splitting a text file into a list of lists
I'm currently attempting to break a .txt file into a list of lists for easier sorting. 我目前正在尝试将.txt文件分解为列表列表,以便于排序。 However, I can not seem to get my splitting function to work.
但是,我似乎无法使我的拆分功能正常工作。 The Text file will look something like this
文本文件将如下所示
First Name Second Name Age Class Room Number GPA
Louis Darter 16 1 3.5
Emma Handzo 15 1 3.6
Jing Luo 25 2 2.0
Charles Xavier 13 2 3.0
Scott Summer 10 1 3.1
And here is what im currently attempting to do: 这是即时通讯目前正在尝试执行的操作:
import csv
reader = csv.reader(open('ARCC_Challenge1.txt', 'rb'), delimiter=' ')
split_text = []
for line in reader:
split_text.append(line[1:-1])
print split_text
Ideally, I'd like to get the output in a format that looks something like [[First Name 1, Last Name 1, Age 1, class room number 1, GPA 1],[First Name 2, Last Name 2, Age 2, class room number 2, GPA 2]...]
Mycurrent code produces an array, but its full of blank spots and many values seem to have \\t
attached to them. 理想情况下,我希望以类似
[[First Name 1, Last Name 1, Age 1, class room number 1, GPA 1],[First Name 2, Last Name 2, Age 2, class room number 2, GPA 2]...]
的格式获取输出。 [[First Name 1, Last Name 1, Age 1, class room number 1, GPA 1],[First Name 2, Last Name 2, Age 2, class room number 2, GPA 2]...]
Mycurrent代码产生的阵列,但其完全空白点和许多值似乎有\\t
附着到它们。 Any ideas that you might be able to give me to accomplish this goal? 您可能会给我实现这一目标的任何想法吗?
I'm guessing your file is tab delimited, not space delimited? 我猜你的文件是制表符分隔的,不是空格分隔的? Try the following:
请尝试以下操作:
import csv
with open('ARCC_Challenge1.txt', 'rb') as fin:
csvin = csv.reader(fin, delimiter='\t') #\t = tab delimited
next(csvin, '') # skip header
split_text = list(csvin)
print map(str.split,open('ARCC_Challenge1.txt', 'rb'))
I think should basically do what you want 我认为基本上应该做你想要的
you may need to call list
on the result depending on the version of python 您可能需要根据结果调用
list
,具体取决于python的版本
alternatively if you are more of a list comprehension guy 或者,如果您更喜欢列表理解者
print [line.split() for line in open('ARCC_Challenge1.txt', 'rb')]
you save your lines instead of printing them 您保存行而不是打印行
lines = ...
#then you can skip some with normal slices
print lines[3:]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.