[英]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. 读取csv文件时遇到问题,该文件用逗号分隔所有内容,但csv文件中的第一个不包含引号。 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",; 符号,“名称”,“最后出售”,“市值”,“ IPO年”,“部门”,“行业”,“摘要报价”,
PIH,"1347 Property Insurance Holdings, Inc.","7.505","$45.23M","2014","Finance","Property-Casualty Insurers"," http://www.nasdaq.com/symbol/pih ",; 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 ",; FLWS,“ 1-800 FLOWERS.COM,Inc。”,“ 9.59”,“ $ 623.46M”,“ 1999”,“消费者服务”,“其他专卖店”,“ 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. 如果csv.reader以单独的列表的形式读取我的文件,并且在每个列表中的所有项目(以逗号分隔)都是它们的单独值,则会发生这种情况。 (eg symbol would be the value of [0], "name" would be the value of [1], etc.)
(例如,符号将是[0]的值,“名称”将是[1]的值,依此类推)
I hope this clears up what I'm looking for 我希望这能弄清楚我在寻找什么
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. 在我的csv文件中没有任何内容的情况下,这样做使得csv.reader可以再次正常读取csv文件。
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. 如果
print(row[0])
给您一个列表,则可能是因为csv文件的每一行都以列表的形式被读取。
try print(row[0][0])
maybe? 尝试
print(row[0][0])
也许?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.