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

标签 python python-3.x

我的代码:

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/

相关文章:

python - Odoo 的日期过滤器无法按预期工作

python-3.x - Python中 'normalize'目录路径的最有效方法是什么?

Python:为什么我不能将 map 对象转换为列表

python - 将嵌套在两个字典下的列表转换为 DataFrame

python - 将列表中的 'continuation' 项分组。在 itertools groupby key 函数中存储状态不好吗?

python - 将 ByteIO 编写为音频 python 以与 Bluemix 一起使用

一个列表理解中多个列表的Python值

python - 如何禁止在 ttk.Combobox tkinter 中输入内容?

python - pip 安装 pygmaps python

python-3.x - OpenCV:如何向整个图像添加人工涂抹/运动模糊效果?