2016-02-13 87 views
1

我正在制作一个脚本来提取cvs文件中的信息。 每列由“;”分隔。 输出应该是包含我想要提取的列的字符串列表。Python:全面列表中的别名

我想和全面的清单要做到这一点,我想这样做:

[ c[1] for c as l.split(";") in for l in lines ] 

如果你知道Python中,你会猜测,这是行不通的。 我怎么能实现这样的事情?

当然,我可以使用[ l.split(";") for l in lines ]但事实上,我需要提取几个列,因此做多个拆分不是正确的选择。

文件看起来像:

115239747;darwin;simone;[email protected];678954312 
112658043;de beauvoir;charles;[email protected];745832259 
115831259;ramanujan;godfrey;[email protected];666443810 
114873956;hardy;srinivasa;[email protected];659332891 
114823401;germain;marguerite;[email protected];768532870 
115821145;yourcenar;sophie;[email protected];645388521 
114560013;harendt;michel;[email protected];666458200 
115702831;foucault;hannah;[email protected];691337456 

,我想提取第二和第三列。

编辑:我不想只使用Python语言功能(无CVS库),因为它是一个关于Python的初学者课程。谢谢。

+0

你把'''与'l'混合在一起。 – Forge

+0

你比我快! ;) –

+1

你的文件看起来像什么?预期的结果是什么? – styvane

回答

4

更新的答案,由于更新问题:

>>> import csv 
>>> from operator import itemgetter 
>>> 
>>> cols = [1,2] # list all the columns you want here 
>>> with open('testfile') as f: 
...  ig = itemgetter(*cols) 
...  result = [ig(row) for row in csv.reader(f, delimiter=';')] 
... 
>>> result 
[('darwin', 'simone'), ('de beauvoir', 'charles'), ('ramanujan', 'godfrey'), ('hardy', 'srinivasa'), ('germain', 'marguerite'), ('yourcenar', 'sophie'), ('harendt', 'michel'), ('foucault', 'hannah')] 

没有进口:

>>> cols = [1,2] # list all the columns you want here 
>>> with open('testfile') as f: 
...  split_lines = [line.split(';') for line in f] 
...  result = [[line[col] for col in cols] for line in split_lines] 
... 
>>> result 
[['darwin', 'simone'], ['de beauvoir', 'charles'], ['ramanujan', 'godfrey'], ['hardy', 'srinivasa'], ['germain', 'marguerite'], ['yourcenar', 'sophie'], ['harendt', 'michel'], ['foucault', 'hannah']] 
+0

对不起,不允许CVS!这是一个Python初学者课程,我们只使用核心功能... –

+0

@NicolasScottoDiPerto添加了一个没有导入的解决方案 – timgeb

+0

我接受它!谢谢你,这很好,我不知道我可以迭代这样的线。但是我仍然在寻找一种在综合列表中寻找别名的方法。可能吗 ?我觉得这个选择很可读。 –

0

[l.split(";")[1] for l in lines ]

0

喜欢这个?

text = "1;2;3\n4;5;6\n;7;8;9" 

col = 1 # for column 1 

L = [row.split(";")[col] for row in [line for line in text.split('\n')]] 

print(L) 
['2', '5', '7'] 
+0

这不完全是这样,我编辑我的帖子更清晰。我正在寻找的关键功能是如何在理解列表中将line.split别名,以便我只能访问一次拆分并多次访问它。 –

1

由于这是一个CSV文件,你需要阅读,为什么不使用csv module:如果你想从拆分数据获得子选择,两种可能的方法

import csv 

with open('file.csv') as csvfile: 
    reader = csv.reader(csvfile, delimiter=";") 
    for row in reader: 
     print(row) 
+0

实际上它是关于学习Python的学校课程,所以这不是选项。对不起 –

+0

@NicolasScottoDiPerto好吧,我虽然认为标准库的'csv'包应该是课程的一部分:) – alecxe

+0

你说得对,或许以后,但现在我们正在开始有关Python的课程所以我们仍然在列表等语言功能... –

0

对于简单情况,您可以使用slice syntax

[l.split(";")[1:3] for l in lines] # will retrieve data from [1,3) range - effectively 1 and 2 

对于更复杂的情况operator.itergetter是一种方法。

返回,从使用 操作数的__getitem__()方法,其操作数取物品的可调用对象。如果指定了多个项目,则 将返回查找值的元组。例如:

import operator 
[operator.itemgetter(1,2)(l.split(";")) for l in lines] # you explicitly pick data with indices 1, 2