gpt4 book ai didi

python - 在 python 中将 .csv 文件打印为列表

转载 作者:行者123 更新时间:2023-11-28 22:38:02 32 4
gpt4 key购买 nike

我的代码:

import csv

with open('serialnumber.csv', 'r') as f:
reader = csv.reader(f, delimiter=',')
your_list = list(reader)

print(your_list)

.csv 文件:

hello,bye

打印输出:

[['hello', 'bye']]

我需要这样的列表:

['hello', 'bye']

我做错了什么?

最佳答案

你得到 [["hello", "bye"]] 的原因是,如果 CSV 有多行,它们将是第一个列表中的项目。

您可以通过访问 your_list 的第一项来获得您想要的内容,如下所示:

>>> print(your_list[0])
["hello", "bye"]

关于python - 在 python 中将 .csv 文件打印为列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35963286/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com