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?

14
  • @cᴏʟᴅsᴘᴇᴇᴅ doesn't work, the same situation like in linked sites :) Commented Aug 17, 2017 at 8:31
  • Can you please add some details? What exactly did you do, that didn't work. Commented Aug 17, 2017 at 8:32
  • i wrote: with open("test.csv", "r") as csvFile: for line in csv.reader(csvFile, quotechar='"', delimiter=',', quoting=csv.QUOTE_ALL, skipinitialspace=True): print(len(line)) ---- and i still get the same values like presented in my main answer Commented Aug 17, 2017 at 8:35
  • i model my code on answer which You linked :) Commented Aug 17, 2017 at 8:37
  • 1
    Key to your problem is that your "csv" isn't a well-formed CSV file. If you can generate it so it is valid then python csv parsing will work. Seems like for your example you need to add quotes around the test="aaa, bbb, ggggg" so it looks like "test="aaa, bbb, ggggg"", but you might get away with simply adding quotes around every value. Commented Aug 17, 2017 at 9:03

1 Answer 1

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')
Sign up to request clarification or add additional context in comments.

6 Comments

Great man! Works! Could You explain me more Your solution? Or link sites, that should i visit to learn?
This is a advantage I planed delete them in further file process
@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)...
this is an output with different format, which was designed on csv.
@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...
|

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.