3
我有這個python腳本將參數作爲由「,」分隔的字符串,但我不能將它分開,因爲有一些參數包含「,」。輸入是這樣的:將包含參數的字符串轉換爲python數組參數
"hello, how are you","how old are you"
,我想,讓他們爲:
["hello, how are you","how old are you"]
我有這個python腳本將參數作爲由「,」分隔的字符串,但我不能將它分開,因爲有一些參數包含「,」。輸入是這樣的:將包含參數的字符串轉換爲python數組參數
"hello, how are you","how old are you"
,我想,讓他們爲:
["hello, how are you","how old are you"]
由於您的字符串看起來像CSV,也許你可以使用csv模塊。
import csv
my_str = '"hello, how are you","how old are you"'
my_csv = [my_str] # Wrap in a list because the csv module expects it
csv_reader = csv.reader(my_csv)
final_array = next(csv_reader)
應該輸出:
[ '你好,你好', '你多大了']
不使用CSV模塊
my_str = '"hello, how are you","how old are you"'
my_str = my_str.split('"')[1::2]
print(my_str)
輸出:
[「你好,你好」,「你多大了」]
那麼,你的輸入實際上是一個字符串'「‘你好,你好’,‘你多大了’」'因爲就像你寫的那樣它已經是2個字符串的元組了......? – Julien
是的,它是一個字符串 –