lindsay-qu
commited on
Create convert_str_to_list.py
Browse files- utils/convert_str_to_list.py +14 -0
utils/convert_str_to_list.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import ast
|
2 |
+
|
3 |
+
def convert_str_to_list(str_input):
|
4 |
+
try:
|
5 |
+
# 使用ast.literal_eval安全地将字符串转换为列表
|
6 |
+
output = ast.literal_eval(str_input)
|
7 |
+
return ast.literal_eval(str_input)
|
8 |
+
except (ValueError, SyntaxError):
|
9 |
+
if str_input[0] == '[' and str_input[1] == '[':
|
10 |
+
output = str_input.lstrip('[').rstrip(']').replace('], [', '],[').split('],[')
|
11 |
+
output = [o.replace('"', "'").replace("\n", "").replace("', '", "','").split("','") for o in output]
|
12 |
+
else:
|
13 |
+
output = str_input.replace('"', "'").replace("\n", "").replace("', '", "','").lstrip("['").rstrip("']").split("','")
|
14 |
+
return output
|