python读取csv格式_python读写csv文件

本文详细介绍了如何使用Python的csv模块处理股票数据,包括读取、写入和引用CSV文件。通过示例代码展示了如何读取股票历史数据,如何引用和格式化数据,以及在处理文件时的不同引用选项。此外,还演示了如何跳过文件头行并打印数据。
摘要由CSDN通过智能技术生成

test文件

"Symbol","Name","LastSale","MarketCap","IPOyear","Sector","Industry","Summary

Quote",

"FAX","Aberdeen Asia-Pacific Income Fund

Inc","7.2","1879027200","1986","n/a","n/a","http://quotes.nasdaq.com/asp/SummaryQuote.asp?symbol=FAX&selected=FAX",

"IAF","Aberdeen Australia Equity Fund

Inc","12.12","232788840","1985","n/a","n/a","http://quotes.nasdaq.com/asp/SummaryQuote.asp?symbol=IAF&selected=IAF",

"CH","Aberdeen Chile Fund,

Inc.","21","213507000","n/a","n/a","n/a","http://quotes.nasdaq.com/asp/SummaryQuote.asp?symbol=CH&selected=CH",

"ETF","Aberdeen Emerging Markets Telecommunications and

Infrastructur","18.23","150324580","1992","n/a","n/a","http://quotes.nasdaq.com/asp/SummaryQuote.asp?symbol=ETF&selected=ETF",

"FCO","Aberdeen Global Income Fund,

Inc.","13.19","118591290","1992","n/a","n/a","http://quotes.nasdaq.com/asp/SummaryQuote.asp?symbol=FCO&selected=FCO",

"IF","Aberdeen Indonesia Fund,

Inc.","12.96","107179200","1990","n/a","n/a","http://quotes.nasdaq.com/asp/SummaryQuote.asp?symbol=IF&selected=IF",

"ISL","Aberdeen Israel Fund,

Inc.","16.6892","71296262.4","1992","n/a","n/a","http://quotes.nasdaq.com/asp/SummaryQuote.asp?symbol=ISL&selected=ISL",

"AXK","Accelr8 Technology

Corporation","4.95","53336250","n/a","Capital

Goods","Biotechnology: Laboratory Analytical

Instruments","http://quotes.nasdaq.com/asp/SummaryQuote.asp?symbol=AXK&selected=AXK",

file = open('test')

file.readline()

读取了一行,下次再引用file的时候,将file的文件指针指向第二行开始的文件.

import csv

writer = csv.writer(file('your.csv', 'wb'))

writer.writerow(['Column1', 'Column2', 'Column3'])

lines = [range(3) for i in range(5)]

for line in lines:

writer.writerow(line)

writer = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC)

现在每个字符串都被引起来了:

$ python csv_writer_quoted.py testout_quoted.csv

$ cat testout_quoted.csv

"Title 1","Title 2","Title 3"

1,"a","08/01/07"

2,"b","08/02/07"

3,"c","08/03/07"

4,"d","08/04/07"

5,"e","08/05/07"

6,"f","08/06/07"

7,"g","08/07/07"

8,"h","08/08/07"

9,"i","08/09/07"

10,"j","08/10/07"

引用

还有4种不同的引用选项, 它们作为常量定义在csv模块中.

QUOTE_ALL

不管是什么类型, 任何内容都加上引号

QUOTE_MINIMAL

这是默认的, 使用指定的字符引用各个域(如果解析器被配置为相同的dialect和选项时,

可能会让解析器在解析时产生混淆)

QUOTE_NONNUMERIC

引用那些不是整数或浮点数的域. 当使用读取对象时, 如果输入的域是没有引号, 那么它们会被转换成浮点数.

QUOTE_NONE

对所有的输出内容都不加引用, 当使用读取对象时, 引用字符看作是包含在每个域的值里(但在正常情况下,

他们被当成定界符而被去掉)

一.文件的读取

文件格式

Date,Open,High,Low,Close,Volume,Adj Close

2011-06-20,3.00,3.09,2.94,3.06,260300,3.06

2011-06-17,2.96,3.07,2.96,3.00,219200,3.00

2011-06-16,3.05,3.11,2.93,2.95,360700,2.95

2011-06-15,3.10,3.15,2.98,3.08,264300,3.08

2011-06-14,3.08,3.25,3.05,3.14,425600,3.14

2011-06-13,3.12,3.18,2.90,2.99,530900,2.99

2011-06-10,3.27,3.33,3.12,3.16,268400,3.16

2011-06-09,3.17,3.38,3.11,3.29,332300,3.29

2011-06-08,3.23,3.27,3.07,3.13,383400,3.13

2011-06-07,3.23,3.35,3.17,3.27,302700,3.27

2011-06-06,3.40,3.41,3.20,3.27,610200,3.27

2011-06-03,3.44,3.54,3.34,3.41,315900,3.41

2011-06-02,3.63,3.66,3.46,3.49,252200,3.49

2011-06-01,3.67,3.80,3.56,3.64,361100,3.64

2011-05-31,3.88,3.88,3.67,3.69,277900,3.69

2011-05-27,3.78,3.89,3.67,3.78,413300,3.78

2011-05-26,3.84,3.92,3.64,3.78,251900,3.78

2011-05-25,3.80,3.92,3.77,3.84,207600,3.84

2011-05-24,3.75,3.90,3.70,3.83,312700,3.83

2011-05-23,3.65,3.88,3.57,3.68,216800,3.68

2011-05-20,3.63,3.80,3.41,3.75,366500,3.75

2011-05-19,3.67,3.73,3.55,3.63,317500,3.63

1.读取的代码

import csv

file = open('/home/pengtao/pythontest/AAU')

file.readline() #读掉第一行,下次再引用file的时候,将file的文件指针指向第二行开始的文件.

reader = csv.reader(file)

for c1,c2,c3,c4,c5,c6,c7 in reader:

print

c7

不读掉第一行的代码

import csv

file = open('/home/pengtao/pythontest/AAU')

reader = csv.reader(file)

for c1,c2,c3,c4,c5,c6,c7 in reader:

print

c7

为何是c1到c7,因为共有7个数据项.

也可以

import csv

file = open('/home/pengtao/pythontest/AAU')

reader = csv.reader(file)

for line in reader:

print

line

2.第一行的处理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值