TXT:https://blog.csdn.net/u010513327/article/details/80889846
EXCEL:https://blog.csdn.net/qq_33683032/article/details/79027811
https://www.cnblogs.com/shaosks/p/6098282.html
import xlrd
import csv
import codecs
# 获取列表的第二个元素
def takeSecond(elem):
return elem[1]
def text_save(filename, data):#filename为写入CSV文件的路径,data为要写入数据列表.
file = open(filename,'a')
for i in range(len(data)):
s = str(data[i]).replace('[','').replace(']','')#去除[],这两行按数据不同,可以选择
s = s.replace("'",'').replace(',','') +'\n' #去除单引号,逗号,每行末尾追加换行符
file.write(s)
file.close()
print("保存文件成功")
def data_write_csv(file_name, datas):#file_name为写入CSV文件的路径,datas为要写入数据列表
file_csv = codecs.open(file_name,'w+','utf-8')#追加
writer = csv.writer(file_csv, delimiter=' ', quotechar=' ', quoting=csv.QUOTE_MINIMAL)
for data in datas:
writer.writerow(data)
print("保存文件成功,处理结束")
if __name__ == '__main__':
dir_case = 'hehe.xlsx'
data = xlrd.open_workbook(dir_case)
table = data.sheets()[0]
nor = table.nrows
nol = table.ncols
dict = {}
temp = []
for i in range(1, nor):
tmp = []
if i == 0: # 跳过第一行
continue
for j in range(nol):
tmp.append(table.cell_value(i, j))
#print(tmp)
temp.append(tmp)
for g in range(0, 8):
print(temp[g])
#sorted(temp,key=takeSecond,reverse=True)
temp.sort(key=takeSecond,reverse=False)
for g in range(0, 8):
print(temp[g])
text_save('hehehahei.txt',temp)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import numpy as np
import os
def listdir(path, list_name):
for file in os.listdir(path):
file_path = os.path.join(path, file)
if os.path.isdir(file_path):
listdir(file_path, list_name)
elif os.path.splitext(file_path)[1] == '.txt':
list_name.append(file_path)
def get_text(filename,temp):
f = open(filename)
line = f.readline()
data_list = []
while line:
num = list(map(float, line.split()))
temp.append(num)
line = f.readline()
f.close()
if __name__ == '__main__':
data_array = []
list_name = []
listdir('F:\\untitled\\11', list_name)
print(list_name)
for str in list_name:
print(str)
filename = str
get_text(filename,data_array)
filename = str
get_text(filename, data_array)
#print(data_array)
for i in data_array:
print(i)