Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

turn string containing arguments into an array with python

I have this python script that takes arguments as strings separated by "," but I cannot just split it because there are some arguments that contain ",". The input is something like this:

"hello, how are you","how old are you"

and I want to get them as:

["hello, how are you","how old are you"]
like image 919
Tsowa Mainasara Avatar asked Aug 06 '26 14:08

Tsowa Mainasara


2 Answers

Since your string looks like csv, maybe you could use the csv module.

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)

Should output:

['hello, how are you','how old are you']

like image 90
A. J. Parr Avatar answered Aug 08 '26 03:08

A. J. Parr


Without using csv module

my_str = '"hello, how are you","how old are you"'
my_str = my_str.split('"')[1::2]
print(my_str)

Outputs:

['hello, how are you', 'how old are you']

like image 39
Van Peer Avatar answered Aug 08 '26 05:08

Van Peer