2011年11月10日星期四

用Python的split输出excel文件


Here’s an example of some data where the dates not formatted well for easy import into Excel:
20 Sep, 263, 1148,   0,   1,   0,   0,   1,   12.1,   13.9, 1+1, 19.9
 20 Sep, 263, 1118,   0,   1,   0, 360,   0,   14.1,   15.3, 1+1, 19.9
 20 Sep, 263, 1048,   0,   1,   0,   0,   0,   14.2,   15.1, 1+1, 19.9
 20 Sep, 263, 1018,   0,   1,   0, 360,   0,   14.2,   15.9, 1+1, 19.9
 20 Sep, 263, 0948,   0,   1,   0,   0,   0,   14.4,   15.3, 1+1, 19.9
The first column has the day and month separated by a space. The second column is year-day, which we’ll ignore. The third column has the time. The data we’re interested in is in the 9th column (temperature). The goal is to have a simple Excel file where the first column is date, and the second column is temperature.
以上从某博客上见到的采用xlrd模块来写excel,感觉很复杂。 自己也编写了一个python脚本来完成相同任务,如下所示:
f = open('weather.data.example.txt','r')
f1= open('result.xls','w')
for line in f:
    L = line.strip().split(',')
    if len(L) < 12:
        continue
    date = L[0].strip()
    time = L[2].strip()
    temperature = str(L[8])
    f1.write(date+'-'+time+'-'+'\t'+temperature+'\n')
print 'OK, finished'

没有评论:

发表评论