梅恋雪 发表于 2022-2-7 13:47:44

想知道这部分代码各个板块是什么意思

#!/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 = matrix.transpose()

            a= np.array(matrix)
            matrix_list = a.tolist()
            matrix_list = matrix_list

            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)
页: [1]
查看完整版本: 想知道这部分代码各个板块是什么意思