[英]Find Nth item in comma separated list in Python
I have a large CSV with comma separated lines of varying length.我有一个带有不同长度的逗号分隔行的大型 CSV。 Sorting another set of data I used
split(',')
in a loop to separate fields, but this method requires each line to have the same number of entries.我在循环中使用
split(',')
对另一组数据进行排序以分隔字段,但此方法要求每行具有相同数量的条目。 Is there a way I can look at a line and, independent of the total number of entries, just pull the Nth item?有没有办法可以查看一行,并且与条目总数无关,只需拉出第 N 个项目? For reference, the method I was using will only work with a line that looks like
AAA,BBB,CCC,DDD
作为参考,我使用的方法仅适用于看起来像
AAA,BBB,CCC,DDD
的行
entry = 'A,B,C,D'
(a,b,c,d) = entry.split(',')
print a,b,c,d
But I would like to pull A and C even if it looks like A,B,C,D,E,F or A,B,C.但我想拉 A 和 C,即使它看起来像 A、B、C、D、E、F 或 A、B、C。
Use a list instead of separate variables.使用列表而不是单独的变量。
values = entry.split(',')
print values[0], values[2]
Just use a list:只需使用一个列表:
xyzzy = entry.split(",");
print xyzzy[0], xyzzy[2]
But be aware that, once you allow the possibility of variable element counts, you'd probably better allow for too few:但请注意,一旦您允许可变元素计数的可能性,您可能最好允许太少:
entry = 'A,B'
xyzzy = entry.split(",");
(a,c) = ('?','?')
if len(xyzzy) > 0: a = xyzzy[0]
if len(xyzzy) > 2: c = xyzzy[2]
print a, c
If you don't want to index the results, it's not difficult to write your own function to deal with the situation where there are either too few or two many values.如果您不想索引结果,编写自己的函数来处理值太少或两个很多的情况并不难。 Although it requires a few more lines of code to set up, an advantage is that you can give the results meaningful names instead of anonymous ones like
results[0]
and results[2]
.虽然它需要几行代码来设置,但优点是您可以为结果提供有意义的名称,而不是像
results[0]
和results[2]
这样的匿名名称。
def splitter(s, take, sep=',', default=None):
r = s.split(sep)
if len(r) < take:
r.extend((default for _ in xrange(take - len(r))))
return r[:take]
entry = 'A,B,C'
a,b,c,d = splitter(entry, 4)
print a,b,c,d # --> A B C None
entry = 'A,B,C,D,E,F'
a,b,c,d = splitter(entry, 4)
print a,b,c,d # --> A B C D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.