gpt4 book ai didi

python - 将列表的字符串表示形式转换为带引号和双引号的列表

转载 作者:行者123 更新时间:2023-12-05 03:51:09 31 4
gpt4 key购买 nike

<分区>

虽然我提到了很多资源,比如 How to convert string representation of list to a list?但我无法在下面解决我的问题。

我的列表如下所示,我将此列表作为列添加到数据框,并保存了数据框。

ls = [['abc'],['a"bcd"e', "ab'cde'"]]
df['list_col'] = ls
df.to_csv('path')

之后,我打开了df dataframe,通过下面的代码,我确认list变成了list的字符串表示。

type(df.list_col[0]) # str

所以我尝试使用下面的代码来表示我的列表的字符串。

import ast
df.list_col = [ast.literal_eval(ls) for ls in df.list_col]
# SyntaxError: EOL while scanning string literal

有什么办法可以解决这个问题吗?

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