Python3 如何读写csv ,csv 模块-新乡seo|网站优化,网站建设_微信公众号:zeropython—昊天博客

csv 格式如下:
Symbol,Price,Date,Time,Change,Volume
"AA",39.48,"6/11/2007","9:36am",-0.18,181800
"AIG",71.38,"6/11/2007","9:36am",-0.15,195500
"AXP",62.58,"6/11/2007","9:36am",-0.46,935000
"BA",98.31,"6/11/2007","9:36am",+0.12,104800
"C",53.08,"6/11/2007","9:36am",-0.25,360900
"CAT",78.29,"6/11/2007","9:36am",-0.23,225400

Python3读取csv 可以利用标准库中的csv 模块:

csv 读取注意事项

Python 2.7 转 Python3 注意事项
// 这里读写方式要把'rb'/'wb'改为'r'/'w'才行
# with open('pingan.csv', 'rb') as rf:
# with open('pingan_copy.csv', 'wb') as wf:
# _csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

// 这里要把reader.next()改为next(reader)才行
# writer.writerow(reader.next())
# '_csv.reader' object has no attribute 'next'

###建议:读取 csv 用namedtuple
它允许你使用列名如 row.Symbolrow.Change 代替下标访问。 需要注意的是这个只有在列名是合法的Python标识符的时候才生效。如果不是的话, 你可能需要修改下原始的列名(如将非标识符字符替换成下划线之类的)。