Python 字符串與列表之間相互轉換
1. 字符串轉列表
test_str = "Hello World"
test_list = test_str.split(" ")
輸出:
['Hello', 'World']
2. 列表轉字符串
test_list = ["Hello", "World"]
test_str = " ".join(test_list)
輸出:
"Hello World"
3.字符串形式列表轉出列表
3.1 利用Python內置函數eval()進行處理
test_str = '["Hello", "World"]'
test_list = eval(test_str)
輸出:
["Hello", "World"]
3.2 利用literal_eval進行轉換
from ast import literal_eval
test_str = '["Hello", "World"]'
test_list = literal_eval(test_str)
輸出:
["Hello", "World"]