3

i had an unique problem. I have code:

with open("test.csv", "r") as csvFile:
reader = csv.reader(csvFile, skipinitialspace=True)
for row in reader:
    for obj in row:
        print(obj)

and exemplary csv file:

anotherCommand, e=5, f=6, g=7, h=9, test="aaa, bbb, ggggg"

i want split this string in this way:

anotherCommand
e=5
f=6
g=7
h=9
test="aaa, bbb, ggggg"

but code which i was presented, split these string in this way:

anotherCommand
e=5
f=6
g=7
h=9
test="aaa
bbb
ggggg"

This is wrong solution this problem. I saw topic like: Why is the Python CSV reader ignoring double-quoted fields? or How can i parse a comma delimited string into a list (caveat)?

But this example is different, and these examples not come up my expectation. Someone have idea?

Robert Pawlak
  • 529
  • 7
  • 23

1 Answers1

1

You could possibly make use of shlex.split here:

import shlex

with open('test.csv') as fin:
    for line in fin:
        row = [col.rstrip(',') for col in shlex.split(line)]
        print(*row, sep='\n')
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • Great man! Works! Could You explain me more Your solution? Or link sites, that should i visit to learn? – Robert Pawlak Aug 17 '17 at 08:58
  • This is a advantage I planed delete them in further file process – Robert Pawlak Aug 17 '17 at 08:59
  • 1
    @Robert as to references - have a look at the documentation for the `shlex` module - it tries to parse text as a shell would when passing command line arguments to a program... (which is what your lines looked more like to me than actual CSV data - so just thought I'd give it a go and see if it produced something workable with for your use case and it mostly did minus retaining trailing commas - hence the `str.rsplit` there)... – Jon Clements Aug 17 '17 at 09:01
  • this is an output with different format, which was designed on csv. – Robert Pawlak Aug 17 '17 at 09:04
  • @Robert Might make sense if you do a `values = dict(col.partition('=')[::2] for col in row)` to get a `dict` of key/values to use if that's applicable for what you want to use it for... – Jon Clements Aug 17 '17 at 09:07
  • @Robert then you get something like: `{'id': '0x1007', 'name': 'EQ_L2_PS_PEER_0, ESP, ON', 'qgroup_addr': '0x31f597c0', 'queue': ''}` - so if the first column is always straight text with key=value, then maybe ignore that, but otherwise, might be a more useful structure... – Jon Clements Aug 17 '17 at 09:10