简体   繁体   English

Python:如何操作列表中的csv数据?

[英]Python: How can I manipulate csv data in a list?

I have created a list which has the totality of all the data in the csv file. 我创建了一个列表,其中包含csv文件中所有数据的总数。
How do I seperately call upon data in rows and columns? 如何分别调用行和列中的数据?

For instance: 例如:

   **a,  b  ,c** 
**1**  a1  b1  c1
**2**  a2  b2  c2

How can I identify a single cell within the list? 如何识别列表中的单个单元格?

try below code: 试试下面的代码:

l = ['a', 'b', 'c','1','a1', 'b1', 'c1', '2', 'a2', 'b2','c2']
columns = 3
result = list(zip(*[iter(l[columns:])]*(columns+1)))
result2 = {i[0]:i[1:] for i in result}
item_id = '2'
result2[item_id]

output: 输出:

 ('a2', 'b2', 'c2')

or you could try below code: 或者您可以尝试以下代码:

l = ['a', 'b', 'c','1','a1', 'b1', 'c1', '2', 'a2', 'b2','c2']
columns = 3
item_id = '2'
index = l.index(item_id)
l[index:index+columns]

output: 输出:

['a2', 'b2', 'c2']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM