3

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"]
2
  • 1
    So is your input actually a single string '"hello, how are you","how old are you"', because as you wrote it it is already a tuple of 2 strings...? Commented Oct 9, 2017 at 0:30
  • yes it is a string Commented Oct 9, 2017 at 0:54

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.