如何正确地从CSV中提取值?python

2024-04-23 08:41:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python3.7

我的代码是:


import csv

file_name = 'num_files/num.csv'

with open(file_name) as file:

    reader = csv.reader(file)

print(reader)

我得到这个结果:

2,5,5,5,33,1,1,9

5
,

Traceback (most recent call last):
  File "/Users/johnkievits/Library/Mobile Documents/com~apple~CloudDocs/python_files/csv_test.py", line 6, in <module>
    reader = csv.reader(file)
AttributeError: module 'csv' has no attribute 'reader'
[Finished in 0.1s with exit code 1]

我有两个问题:

第一:昨天代码运行良好。只需要两条第一行。 “回溯”在傍晚突然出现。这怎么可能?你知道吗

第二:我的目标是从第一行得到te值。他们现在把自己呈现为:

2、5、5、33、1、1、9

第二行的“5”不是值! 我想让每一个值都分配给一个变量。你知道吗

还有一件奇怪的事。当我在终端运行代码时(我使用mac),我没有得到回溯错误! 只有两行出现。其中第二行不是csv文件中的值!你知道吗

我希望有一个更有经验的用户可以帮助我重新上路,帮助我解决这两个问题。谢谢!你知道吗


Tags: csv代码nameinimportaswithfiles