python 该脚本用来读取excel表格指定数据并返回所在行列值, 将对应行列值的txt文件读取归档到gsmap_result文件夹

10 篇文章 0 订阅
6 篇文章 0 订阅
# -coding: utf-8 -*
"""
该脚本用来读取excel表格指定数据并返回所在行列值,
将对应行列值的txt文件读取归档到gsmap_result文件夹
A: 大于1500
B: 1300-1500
C:小于1300
"""
import pandas as pd
import os
import numpy as np
import shutil


def read_txt(path):

    filelist = os.listdir(path)
    file_names = np.array([file.split('_')[1] for file in filelist if file.endswith('.txt')], dtype=object)

    return filelist,file_names

#对数据进行分区
def divide2A_B_C(path_excel):
    df = pd.read_excel(path_excel)
    A = []
    B = []
    C = []
    index_A = []
    index_B = []
    index_C = []
    for row in range(df.shape[0]):
        for col in range(df.shape[1]):
            if df.ix[row][col] >= 1500 and df.ix[row][col] != -9999:
                A.append(df.ix[row][col])
                index_A.append(('%02d' + '%02d') % (row + 2, col + 1))
            elif 1300 <= df.ix[row][col] < 1500:
                B.append(df.ix[row][col])
                index_B.append(('%02d' + '%02d') % (row + 2, col + 1))
            else:
                C.append(df.ix[row][col])
                index_C.append(('%02d' + '%02d') % (row + 2, col + 1))
    return index_A, index_B, index_C

#找寻相同后缀名文件并复制到相应文件夹
def findall_file(divide_list, filelist, filename, path_result_x, path_txt):
    for i in divide_list:
        if i+'.txt' in filename:
            src = path_txt + filelist[filename.index(i+'.txt')]
            dst = path_result_x + filelist[filename.index(i+'.txt')]
            shutil.copy(src, dst)

path_excel = 'DEM.xls'
path_txt = './gsmap_mvk/'
path_result = './gsmap_result/'
path_result_A = './gsmap_result/A/'
path_result_B = './gsmap_result/B/'
path_result_C = './gsmap_result/C/'

filelist,filename = read_txt(path_txt)
filename = list(filename)
x = filelist[filename.index('0212.txt')]
print(x)
index_A, index_B, index_C = divide2A_B_C(path_excel)


findall_file(index_A, filelist, filename, path_result_A, path_txt)
findall_file(index_B, filelist, filename, path_result_B, path_txt)
findall_file(index_C, filelist, filename, path_result_C, path_txt)



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值