这是我提取数据的脚本:
def graphData(stock):
try:
print 'Currently pulling',stock
fileLine = stock+'.txt'
urlToVisit = 'http://mis.tse.com.tw/data/'+stock+'.csv'
sourceCode = urllib2.urlopen(urlToVisit).read()
splitSource = sourceCode.split('\n')
for eachLine in splitSource:
splitLine = eachLine.split(',')
if len(splitLine)==40:
saveFile = open(fileLine,'a')
eachLine=eachLine.replace('"','')
lineToWrite = eachLine+'\n'
saveFile.write(lineToWrite)
f=open(fileLine,'r')
line = f.readline()
alist = line.split(',')
f.close()
except Exception, e:
print str(e), 'failed to organize pulled data'
for eachStock in stocksToPull:
graphData(eachStock)
并保存为txt.file,但数据的内容太长,看起来像这样:
2317,-0.3,10:38:43,92.2,80.2,86.1,86.1,85.8,85.9,9741,11,85.9,519,85.8,325,85.7,266,85.6,216,85.5,464,86,482,86.1,344,86.2,374,86.3,196,86.4,129,0,1,31,1000,13128706840,name,,,
但我只需要前9个变量,我怎么能把这条丑陋的线条组织成干净的线条并保存为新文件或者重写旧文件?