本程序需要结合文件名提取脚本和多文件数据提取文件脚本操作。
源代码如下:
#!usr\bin\python
# -*-coding:utf-8 -*-
import csv
import pandas as pd
import numpy as np
import winreg
import os
'''
1.打开链表文件,依次读取所要提取的数据
'''
'''******************链表文件数据和个数提取******************'''
def listfilehandle(listfile):
Rowdata = []
rfile = open(listfile, 'r')
rfilelist = rfile.readlines()#直接读取文件中每一行数据转换成List类型
#ListFileLen = len(rfilelist) # 链表文件中文件名的个数
#print ListFileLen
for filename in rfilelist:
Rowdata.append(filename.strip('\n'))
return Rowdata
'''******************CSV文件名打开路径列表*****************'''
def fnlistpath(filename):
root = 'C:\Desktop\CSV\NewData\DataValue'#转换后的csv数据所在的目录
fileopenpath = root + '\\' + filename
return fileopenpath
'''***********************特定数据提取********************************'''
def get_desktop():
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders')#利用系统的链表
return winreg.QueryValueEx(key, "Desktop")[0] #返回的是Unicode类型数据
def GetCsvAllData(filepath):#读取CSV所有数据
#filetype = '.csv'
#filepath = root + '\\' + filename + filetype
data = pd.read_csv(filepath, header=None, index_col=None)
return data
def GetCsvColData(data , Col):#读取Col=1列数据
getcoldata = data.ix[: , Col] # 读取所有行,Col列数据,此处固定第一列(表示扫描电压值)Col=1
return getcoldata
def DataToList(data):#只转换一列数据为List类型
arrsdata = data.values # Col列数据的值存放在数组中
listdata = arrsdata.tolist() # array类型转换为list类型
return listdata
def GetSatisfyDataRowList(listdata, satisfyvalue):#得到满足数据的所有行,转换为List类型
getdatalist = [] # 参考列下所有数据列表
getrowlist = [] # 满足条件的行号列表
for idata_list in listdata:#主要对参考列的数据进行曲有效数据位,方便数据检索(可去除,视情况而定)
fidata_list = float("%.4f" % idata_list) # 仅显示4位有效数据,且转换为浮点型
getdatalist.append(fidata_list)
for ilb in range(len(listdata)):
if getdatalist[ilb] == satisfyvalue:
getrowlist.append(ilb)
#print ilb
return getrowlist
def dataclassify(datalist):
Singdata = []
Doubdata = []
datalen = len(datalist)
for item in range(datalen):
if item % 2 == 0:
Singdata.append(datalist[item])
else:
Doubdata.append(datalist[item])
return Singdata,Doubdata
if __name__=='__main__':
alllistdata = []
getdataCol = raw_input('dataCol(you want):')
satisfyvalue = raw_input('Threshold:')
if getdataCol=='':
getdataCol = '3'#默认提取第四列(一般是采集的电阻数据)
if satisfyvalue == '':
satisfyvalue = '-0.1'#提取-0.1V电压下的电阻值
getdataCol = int(getdataCol)
satisfyvalue = float(satisfyvalue)
print('提取电压值为%fV,第%d列数据' %(satisfyvalue,getdataCol))
Desktop_path = str(get_desktop())
listfilesroot = Desktop_path +'\CSV\NewData\listfile.txt'#CSV文件名所在的TXT文件
Rowdata = listfilehandle(listfilesroot)
mkfilepath = Desktop_path + '\CSV' # 在电脑桌面创建CSV文件夹
HRS = mkfilepath + '\NewData\DataValue\\' + 'aHRS.txt'
LRS = mkfilepath + '\NewData\DataValue\\' + 'aLRS.txt'
with open(HRS, 'w+') as fo:
fo.truncate() # 清空fo文件内容
with open(LRS, 'w+') as foo:
foo.truncate() # 清空foo文件内容
for index in Rowdata:
filepath = fnlistpath(index)
'''fnlist = os.walk(root)#文件夹下的文件夹操作(后续进一步开发)
for root, dirs, files in fnlist:
print dirs'''
getdata = GetCsvAllData(filepath)
listdata = DataToList(GetCsvColData(getdata,1))
getrowlist = GetSatisfyDataRowList(listdata,satisfyvalue)
goaldata = getdata.ix[getrowlist,getdataCol]
ListColData = DataToList(goaldata)
SatisfyRowSingdata,SatisfyRowDoubdata = dataclassify(ListColData)
print SatisfyRowSingdata
print SatisfyRowDoubdata
print '************'+index+'**************'
#数据写入到TXT文件中
fo = open(HRS, 'a')
for fSingdata in SatisfyRowSingdata:
#fSingdata = abs(fSingdata)
fo.write(str(fSingdata)+'\r\n')
fo.close()
foo = open(LRS, 'a')
for fDoubdata in SatisfyRowDoubdata:
#fDoubdata = abs(fDoubdata)
foo.write(str(fDoubdata)+'\r\n')
foo.close()
'''写入CSV列数据,以字典形式写入'''
'''alllistdata.append(listdata)
dataframe = pd.DataFrame(zip())
dataframe.to_csv("C:\Users\yujie\Desktop\CSV\NewData\\123\\" + index, index=False, sep=',') # header=None,'''
''' for fdata in SatisfyRowListData:#改为csv存储数据
fo.write(str(fdata)+'\r\n')'''