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:
c7
不读掉第一行的代码
import csv
file = open('/home/pengtao/pythontest/AAU')
reader = csv.reader(file)
for c1,c2,c3,c4,c5,c6,c7 in reader:
c7
为何是c1到c7,因为共有7个数据项.
也可以
import csv
file = open('/home/pengtao/pythontest/AAU')
reader = csv.reader(file)
for line in reader:
line
2.第一行的处理