Tsowa Mainasara
Tsowa Mainasara

Reputation: 93

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"]

Upvotes: 3

Views: 79

Answers (2)

Van Peer
Van Peer

Reputation: 2167

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']

Upvotes: 1

A. J. Parr
A. J. Parr

Reputation: 8026

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']

Upvotes: 3

Related Questions