读取用逗号分隔所有内容的csv文件时出现问题,但csv文件中的第一个文件不包含引号。例如:
Symbol,"Name","LastSale","MarketCap","IPOyear","Sector","industry","Summary Quote",
用于尝试和阅读的代码如下:
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])
我得到的是以下内容:
Symbol,"Name","LastSale","MarketCap","IPOyear","Sector","industry","Summary Quote",;
但是,我只是想从这个列表中获取所有符号。有人知道怎么做吗?
编辑
更多数据:
符号,"名称"" LastSale"" MarketCap"" IPOyear""扇区#34 ;, "行业","摘要报价",;
PIH," 1347 Property Insurance Holdings,Inc。"," 7.505"," $ 45.23M"," 2014", "财务","财产 - 伤亡保险公司"," http://www.nasdaq.com/symbol/pih",
FLWS," 1-800 FLOWERS.COM,Inc。"," 9.59"," $ 623.46M"," 1999&#34 ;,"消费者服务","其他专卖店"," http://www.nasdaq.com/symbol/flws",
所以我的预期输出是:
Symbol
PIH
FLWS
如果csv.reader将我的文件作为单独的列表读取每个行,并且在每个列表中,所有项目(以逗号分隔)将是它们的单独值,则会发生这种情况。 (例如,符号将是[0]的值,"名称"将是[1]的值,等等。)
我希望这能清除我正在寻找的东西
答案 0 :(得分:2)
找到了简单的方法:
取代所有
"
在我的csv文件中使用 nothing ,这使得csv.reader可以再次正常读取csv文件。
答案 1 :(得分:0)
如果print(row[0])
给你一个列表,可能是因为你的csv文件的每一行都是作为一个列表读入的。
尝试print(row[0][0])
可能吗?