读取Python中CSV文件中每个点的数据列
我想阅读带有标题nodes
的第二列数据列,并分配给每个点t1
的相同名称的变量。
import csv
with open('Data_10x10.csv', 'r') as f:
csv_reader = csv.reader(f)
数据看起来像
I want to read the second column of data with the title nodes
and assign to a variable with the same name for each point of t1
.
import csv
with open('Data_10x10.csv', 'r') as f:
csv_reader = csv.reader(f)
The data looks like
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
csv_reader = csv.reader(f)
是 Generator 。因此,您可以通过执行heading = Next(CSV_Reader)
跳过标题。我只会使用字典
data_t1
用于使用列的关键名称t1
存储节点数据。在下面尝试一下。
访问数据(在这种情况下,键是t1列的值,在这种情况下为'0','1'等)
print(data_t1 ['0'])
print(data_t1 ['1'])
如果要为每个点具有相同名称的动态变量,
t1
这真的是个坏主意。如果您的CSV有很多行,则可能会产生数百万个变量。因此,使用字典
带有键和值。csv_reader = csv.reader(f)
is a Generator. So you can skip the headers by executeheading = next(csv_reader)
.I would just use a dictionary
data_t1
for storing node data with key name of columnt1
.Try below one.
Accessing data (key should be value of you t1 column, in this case '0', '1' etc.)
print(data_t1['0'])
print(data_t1['1'])
If you want to create dynamic variables with the same name for each point of
t1
It is really bad idea. If your csv has lot of rows maybe millions, it will create millions of variables. So usedictionary
with key and values.