1

I would like to separate a string that looks like this

'190-1\tbla-bla\tall_right\t"dont\tseparate me"\toooo'). 

The question how can I split it by tabs not concerning what is in quotations "". So the output is:

190-1,  bla-bla,   all_right,  "dont\tseparate me",  oooo

The answer was given by Scott Mermelstein:

line for line in csv.reader(infile):

Note the [] around the string in the answer on the other page.

EDIT

Answer was by @Scott Mermelstein

use the line for line in csv.reader(infile, delimiter='\t'): If this gets reopened, I'll convert this comment to an answer.

Note the [] around the string in the answer on the other page. If your line is only infile = '190-1\tbla-bla\tall_right\t"dont\tseparate me"\toooo', you'll get the result you describe. If your line is infile = ['190-1\tbla-bla\tall_right\t"dont\tseparate me"\toooo'] and you modify the reader line as above, you'll get the right result

Marcel Mars
  • 389
  • 5
  • 15
  • @Wiktor_Stribizew I don't think duplicate question answers it. – R__raki__ Nov 15 '16 at 14:58
  • It doesn't answer my question. – Marcel Mars Nov 15 '16 at 15:03
  • I nominated this for reopening. While we can use a CSV reader, using the answer from "the dupe question" doesn't do it on its own. We at least need to tell the reader to use tab as the separator. – Scott Mermelstein Nov 15 '16 at 15:05
  • 1
    @Marcel - in the interest in getting you moving, refer to the "dupe answer", and instead of the line `for line in csv.reader(infile):`, use the line `for line in csv.reader(infile, delimiter='\t'):` If this gets reopened, I'll convert this comment to an answer. – Scott Mermelstein Nov 15 '16 at 15:06
  • It doesn't work as well. It takes each character instaed of separating using delimiter. – Marcel Mars Nov 15 '16 at 16:26
  • 1
    Note the `[]` around the string in the answer on the other page. If your line is only `infile = '190-1\tbla-bla\tall_right\t"dont\tseparate me"\toooo'`, you'll get the result you describe. If your line is `infile = ['190-1\tbla-bla\tall_right\t"dont\tseparate me"\toooo']` and you modify the reader line as above, you'll get the right result. – Scott Mermelstein Nov 15 '16 at 16:30
  • Yes,adding brackets indeed that worked. I add it into edit. – Marcel Mars Nov 17 '16 at 11:59

0 Answers0