如何从CSV文件中获取值并通过列迭代

发布于 2025-02-07 00:14:05 字数 465 浏览 2 评论 0原文

我的CSV文件示例:

'Date','Category','Ability'
'21,2,5','Sparrow','Air,land'
'4,5,6','Eagle','Air,Land'

我当前的代码:

with open(Filepath,'r') as f :
 user_read=csv.reader(f)
 dict_date=[line['Date'] for line in user_read]
print(dict_date)

错误:

typeerror:列表索引必须是整数或切片,而不是str

我的预期输出:

[21,2,5,4,5,6]

任何想法..所以我的类别和能力具有单独的字典

My csv File sample :

'Date','Category','Ability'
'21,2,5','Sparrow','Air,land'
'4,5,6','Eagle','Air,Land'

My current code:

with open(Filepath,'r') as f :
 user_read=csv.reader(f)
 dict_date=[line['Date'] for line in user_read]
print(dict_date)

Error :

TypeError : list indices must be integer or slices,not str

My expected Output :

[21,2,5,4,5,6]

Any ideas.. So my Category and Ability has seperate Dictionary

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

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

发布评论

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

评论(4

耶耶耶 2025-02-14 00:14:06

尝试:

import csv


all_data = []
with open("data.csv", "r") as f_in:
    reader = csv.reader(f_in, quotechar="'")

    next(reader)  # skip header

    for row in reader:
        all_data.extend(map(int, row[0].split(",")))

print(all_data)

打印:

[21, 2, 5, 4, 5, 6]

Try:

import csv


all_data = []
with open("data.csv", "r") as f_in:
    reader = csv.reader(f_in, quotechar="'")

    next(reader)  # skip header

    for row in reader:
        all_data.extend(map(int, row[0].split(",")))

print(all_data)

Prints:

[21, 2, 5, 4, 5, 6]
一生独一 2025-02-14 00:14:06

您可以使用Pandas来完成它:

import pandas as pd

df = pd.csv_reader("input.csv")
output = df['Date'].to_list()

输出将为:

['21,2,5', '4,5,6']

You can accomplice it using Pandas:

import pandas as pd

df = pd.csv_reader("input.csv")
output = df['Date'].to_list()

The output will be:

['21,2,5', '4,5,6']
海螺姑娘 2025-02-14 00:14:06

由于政策限制,我没有使用大熊猫
在对我有用的代码下方

Date_values=[]
with open(users_read,'r') as f:
  reader=DictReader(f)
  for row in reader:
    Date_values+=row['Date'].split(",")
  print(Date_values)

I didnt use pandas due to policy restriction
Below the code which worked for me

Date_values=[]
with open(users_read,'r') as f:
  reader=DictReader(f)
  for row in reader:
    Date_values+=row['Date'].split(",")
  print(Date_values)
万劫不复 2025-02-14 00:14:05

您正在按dict访问行,可以通过 csv.dictreader,而且您也没有设置 quodechar 对于您正在使用的方言,默认为>。“”。

以下方面确实可以做到这两个,并且以您想要的方式工作。

import csv
from io import StringIO

# I’m using StringIO to avoid writing a file,
# if the data is in a file keep using with open(…) as f
buf = StringIO(
    """\
'Date','Category','Ability'
'21,2,5','Sparrow','Air,land'
'4,5,6','Eagle','Air,Land'
"""
)

with buf as f:
    reader = csv.DictReader(f, quotechar="'")
    date = [
        int(v) for row in reader for v in row["Date"].split(",")
        ]

print(date)  # [21, 2, 5, 4, 5, 6]

You're accessing the rows as dicts, which is available through csv.DictReader, and also you're not setting the quotechar for the dialect you're using which therefore defaults to ".

The following does both and works in the way you want.

import csv
from io import StringIO

# I’m using StringIO to avoid writing a file,
# if the data is in a file keep using with open(…) as f
buf = StringIO(
    """\
'Date','Category','Ability'
'21,2,5','Sparrow','Air,land'
'4,5,6','Eagle','Air,Land'
"""
)

with buf as f:
    reader = csv.DictReader(f, quotechar="'")
    date = [
        int(v) for row in reader for v in row["Date"].split(",")
        ]

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