读取Python中CSV文件中每个点的数据列

发布于 2025-02-12 19:29:53 字数 372 浏览 1 评论 0原文

我想阅读带有标题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

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南渊 2025-02-19 19:29:53

csv_reader = csv.reader(f) Generator 。因此,您可以通过执行heading = Next(CSV_Reader)跳过标题。

我只会使用字典data_t1用于使用列的关键名称t1存储节点数据。

在下面尝试一下。

import csv

with open('Data_10x10.csv', 'r') as f:
    data_t1={}
    csv_reader = csv.reader(f)
    # Skips the heading
    heading = next(csv_reader)

    for row in csv_reader:
        data_t1[row[0]] = row[1]

访问数据(在这种情况下,键是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 execute heading = next(csv_reader).

I would just use a dictionary data_t1 for storing node data with key name of column t1.

Try below one.

import csv

with open('Data_10x10.csv', 'r') as f:
    data_t1={}
    csv_reader = csv.reader(f)
    # Skips the heading
    heading = next(csv_reader)

    for row in csv_reader:
        data_t1[row[0]] = row[1]

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 use dictionary with key and values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文