0

I have a csv file that contains

"VANS, PASSENGER TYPE",CHEVROLET,H1500 EXPRESS AWD,5.3,8,Auto(L4),4,832,9,12,10,11.5,16.2,13.2268,E,,,,,,,,,,3900,310-340,CLKUP ,2,15,30-Jun-07,DERIVED

All I am trying to do is split by comma, do only problem with my current solution is that the first entry which happens to be "VANS, PASSENGER TYPE" has a comma but I am not interested in splitting that.

Currently I am doing something like this

with open("file.txt", "r") as ins:
   foo = ins.split(",")
2
  • 3
    If you have a CSV file, why not use the csv module? Commented Mar 27, 2015 at 2:24
  • did not know about that module.... Looks interesting...Let me try that out! Commented Mar 27, 2015 at 2:25

1 Answer 1

6

It's better to use csv module.

import csv
with open('file') as f:
    reader = csv.reader(f)
    for line in reader:
        print(line)

Output:

['VANS, PASSENGER TYPE', 'CHEVROLET', 'H1500 EXPRESS AWD', '5.3', '8', 'Auto(L4)', '4', '832', '9', '12', '10', '11.5', '16.2', '13.2268', 'E', '', '', '', '', '', '', '', '', '', '3900', '310-340', 'CLKUP ', '2', '15', '30-Jun-07', 'DERIVED']
Sign up to request clarification or add additional context in comments.

Comments

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.