Menno Van Dijk
Menno Van Dijk

Reputation: 903

CSV file with comma delimiter and quotes, but not on every line

Having an issue with reading a csv file that delimits everything with commas, but the first one in the csv file does not contain quotes. Example:

Symbol,"Name","LastSale","MarketCap","IPOyear","Sector","industry","Summary Quote",

the code used to try and read this is as follows:

from ystockquote import *
import csv

with open('companylist.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL)

for row in readCSV:
        print(row[0])

What I get is the following:

Symbol,"Name","LastSale","MarketCap","IPOyear","Sector","industry","Summary Quote",;

However, I just want to get all of the symbols from this list. Anyone an idea on how to do this?

edit

More data:

Symbol,"Name","LastSale","MarketCap","IPOyear","Sector","industry","Summary Quote",;

PIH,"1347 Property Insurance Holdings, Inc.","7.505","$45.23M","2014","Finance","Property-Casualty Insurers","http://www.nasdaq.com/symbol/pih",;

FLWS,"1-800 FLOWERS.COM, Inc.","9.59","$623.46M","1999","Consumer Services","Other Specialty Stores","http://www.nasdaq.com/symbol/flws",;

So my expected output would be:

Symbol
PIH
FLWS

This would happen if the csv.reader read my file as each of the rows as a seperate list, and within each of these lists all of the items (delimited by commas) would be their seperate values. (e.g. symbol would be the value of [0], "name" would be the value of [1], etc.)

I hope this clears up what I'm looking for

Upvotes: 1

Views: 178

Answers (2)

Menno Van Dijk
Menno Van Dijk

Reputation: 903

Found the easy way out:

Replaced all of the

"

with nothing in my csv file, this made it so that the csv.reader could read the csv file normally again.

Upvotes: 2

RichSmith
RichSmith

Reputation: 940

If print(row[0]) is giving you a list, it might be because each row of your csv file is being read in as a list.

try print(row[0][0]) maybe?

Upvotes: 0

Related Questions