3

I'm desperatly trying to split a string using Python but the text file I need to parse is a little bit tricky:

  • The text file is a comma separated data file

I did the following:

import fileinput
for line in fileinput.input("sample.txt"):
data = line.strip().split(',')
pass

This actually should make the job right ?

Ok now the tricky part: I have some field that contains comma inside like the one below:

"(CONTRACTS OF 5,000 BUSHELS)"

using my code, the script also split this field in 2.

How can I ask python to use comma as a delimiter but not when they are enclosed by "" ?

Thank you in advance for your answers

Crak

2
  • Why do you have a pass in your code example? Commented Jun 1, 2011 at 13:16
  • It seems that the solution was in the question: The text file is a comma separated data file. I was confused because of the file extension which was a .txt... One day lost for almost nothing :) Thanks. Commented Jun 1, 2011 at 13:58

3 Answers 3

10

Your data is in a pretty common format -- Comma Separated Values (CSV). Instead of writing yet another parser, consider using the built-in csv module.

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

5 Comments

Seems like good advice, but an example of how the csv module would be used in this case would really improve this answer.
There are plenty of examples in the official documentation, I don't think copying one of those here makes a difference.
Maybe it's because I've never used a csv library that after several minutes of reading the docs left me confused. You are right, you have provided what is necessary. But it would have made a difference for me, if it were my problem.
@FarmBoy: There are hundreds of questions about CSV already on Stack Overflow. Please search for Python and CSV. There's no point in repeating any of those answers on this question.
@FarmBoy: Please click on the link to the csv module in my answer and scroll down a couple of lines to the first green box for a very straight-forward example. And please, be polite, I totally agree with @S.Lott's comment.
5

CSV files should be handled with csv.

Comments

4

you can use the csv module

import csv

with open('sample.txt', 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
        # each row is a list of items,
        # corresponding to each row in your file,
        # including commas for quoted items

3 Comments

I'd upvote this answer, if it contained something meaningful where the comment is.
the question didn't specify what he is doing with it
He is splitting on commas, but not splitting on commas within quotations.

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.