|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#!/usr/bin/env python
# coding: utf-8
import pandas as pd
import numpy as np
import linecache
import csv
# 读取数据
yearnum = 2018
monfilenum = 0
daynum = 0
# yearPath = r'D:\bishe\ASCII\China\tem' + str(yearnum) + '\\' + 'temperature_' + str(yearnum)
yearPath = r'D:\bishe\ASCII\China\tem' + str(yearnum) + '\\' + 'temperature_' + str(yearnum)
hourList=[]
for monfilenum in range (12):
monfilenum = monfilenum + 1
strmon = '%02d' % monfilenum
monPath = yearPath + strmon
if monfilenum == 2:
if yearnum % 4 == 0:
daynum=29
else:
daynum=28
elif monfilenum==4 or monfilenum==6 or monfilenum==9 or monfilenum==11:
daynum=30
else:
daynum=31
###每月按天循环读取
for dd in range (daynum):
dd = dd + 1
strday = '%02d' % dd
dayPath = monPath + strday +'-band_'
###每日24小时循环读取
for j in range (24):
path = ''
path = dayPath + str(j+1) +'.txt'
# print(path)
df = ''
###前6行不读取,读取7至83行数据
for i in range (77):
df += linecache.getline(path,i+7).strip()
df += ' '
nums = df.split(' ')
## 添加到 matrix 中。
first_ele = True
if first_ele:
### 加入到 matrix 中 。
matrix = np.array(nums)
first_ele = False
else:
matrix = np.c_[matrix,nums]
matrix = matrix.transpose()
a= np.array(matrix)
matrix_list = a.tolist()
matrix_list = matrix_list[0:-1]
new_numbers = [];
for n in matrix_list:
##删除 '-9999'
if n !='-9999':
new_numbers.append(float(n));
new_list = new_numbers;
hourList = hourList + new_list
###导出数据
test=pd.DataFrame(data=hourList)
test.to_csv('D:/bishe/CSV/China/tem20182.csv',encoding='gbk')
len(hourList)
|
|