如何从导入的 csv 文件中删除单引号到列表中?
我将列表导出到 csv 文件中,如下所示:
file = open('all_states.csv', 'w+', newline='')
# writing the data into the file
with file:
write = csv.writer(file)
write.writerows(all_states)
从另一个脚本中,我将 csv 文件导入为:
with open('all_states.csv') as file:
reader = csv.reader(file)
states = list(reader)
file.close()
导出的原始数据是:
exp_data = [
[[0], [[1, 0, 0], [1, 0, 0], [0, 1, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 0], [0, 1, 0]], [0, 0, 1]],
[[0], [[1, 0, 0], [1, 0, 0], [0, 1, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 0], [0, 1, 0]], [-1, 0, 0]],
[[0], [[1, 0, 0], [1, 0, 0], [0, 1, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 0], [0, 1, 0]], [0, -1, 0]]
]
但是当我导入时,我得到了一些我想删除的单引号。
impo_data = [
['[0]', '[[1, 0, 0], [1, 0, 0], [0, 1, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 0], [0, 1, 0]]', '[0, 0, 1]'],
['[0]', '[[1, 0, 0], [1, 0, 0], [0, 1, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 0], [0, 1, 0]]', '[-1, 0, 0]'],
['[0]', '[[1, 0, 0], [1, 0, 0], [0, 1, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 0], [0, 1, 0]]', '[0, -1, 0]']
]
如何在导出或导入时删除引号?
I export a list into a csv file as:
file = open('all_states.csv', 'w+', newline='')
# writing the data into the file
with file:
write = csv.writer(file)
write.writerows(all_states)
And from another script I import the csv file as:
with open('all_states.csv') as file:
reader = csv.reader(file)
states = list(reader)
file.close()
The original data that was exported was:
exp_data = [
[[0], [[1, 0, 0], [1, 0, 0], [0, 1, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 0], [0, 1, 0]], [0, 0, 1]],
[[0], [[1, 0, 0], [1, 0, 0], [0, 1, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 0], [0, 1, 0]], [-1, 0, 0]],
[[0], [[1, 0, 0], [1, 0, 0], [0, 1, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 0], [0, 1, 0]], [0, -1, 0]]
]
But when I imported I get some single quotes that I'd like to remove.
impo_data = [
['[0]', '[[1, 0, 0], [1, 0, 0], [0, 1, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 0], [0, 1, 0]]', '[0, 0, 1]'],
['[0]', '[[1, 0, 0], [1, 0, 0], [0, 1, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 0], [0, 1, 0]]', '[-1, 0, 0]'],
['[0]', '[[1, 0, 0], [1, 0, 0], [0, 1, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 0], [0, 1, 0]]', '[0, -1, 0]']
]
How can I remove the quotes when exporting or once imported?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您的 CSV 文件被作为字符串读取,而您希望将其视为字符串而不是字符串。您的项目看起来都是 JSON,因此只需通过 json.loads 运行它们即可。
The issue is that your CSV file is read as a string, while you want to process it as if it is not. Your items look like they are all JSON, so just run them through
json.loads
.据我所知,csv格式的引用没问题。但如果您想删除引号,可以尝试以下操作。导入 csv 文件时,定义 quotechar:
As far as I know, quotations are fine with csv format. But if you want to remove the quotes, you can try the following. While importing the csv file, define the quotechar: