如何从导入的 csv 文件中删除单引号到列表中?

发布于 2025-01-17 06:48:55 字数 1283 浏览 1 评论 0原文

我将列表导出到 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 技术交流群。

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

发布评论

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

评论(2

北方。的韩爷 2025-01-24 06:48:55

问题是您的 CSV 文件被作为字符串读取,而您希望将其视为字符串而不是字符串。您的项目看起来都是 JSON,因此只需通过 json.loads 运行它们即可。

states = [[json.loads(item) for item in row] for row in reader]

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.

states = [[json.loads(item) for item in row] for row in reader]
萌酱 2025-01-24 06:48:55

据我所知,csv格式的引用没问题。但如果您想删除引号,可以尝试以下操作。导入 csv 文件时,定义 quotechar

csv.reader(inp_file, delimiter=',', 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:

csv.reader(inp_file, delimiter=',', quotechar="'")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文