I have some input that looks like the following:
A,B,C,"D12121",E,F,G,H,"I9,I8",J,K
The comma-separated values can be in any order. I'd like to split the string on commas; however, in the case where something is inside double quotation marks, I need it to both ignore commas and strip out the quotation marks (if possible). So basically, the output would be this list of strings:
['A', 'B', 'C', 'D12121', 'E', 'F', 'G', 'H', 'I9,I8', 'J', 'K']
I've had a look at some other answers, and I'm thinking a regular expression would be best, but I'm terrible at coming up with them.
split("(? =\"[^\"]. *\")");
To split a string with comma, use the split() method in Java. str. split("[,]", 0);
replace your string. split(",") by string. split(", ") with a space after the comma. This should be enough to avoid splitting the numbers.
Lasse is right; it's a comma separated value file, so you should use the csv
module. A brief example:
from csv import reader # test infile = ['A,B,C,"D12121",E,F,G,H,"I9,I8",J,K'] # real is probably like # infile = open('filename', 'r') # or use 'with open(...) as infile:' and indent the rest for line in reader(infile): print line # for the test input, prints # ['A', 'B', 'C', 'D12121', 'E', 'F', 'G', 'H', 'I9,I8', 'J', 'K']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With