#-*-encoding:utf-8-*-
import os, sys, time, datetime, glob
fo1 = open("C:/Users/Administrator/Desktop/areainfo.txt", "w")
fo2 = open("C:/Users/Administrator/Desktop/personinfo.txt", "w")
for root, dirs, files, in os.walk("E:/test", topdown=False):
for name in files:
file_name = os.path.join(root,name)
if os.path.splitext(file_name)[1] == '.txt':
print(file_name)
with open(file_name) as a_file:
for data in a_file.readlines():
str2=[]
str4=[]
for i in [0, 1, 2, 3, 7]:
str2.append(data.split('||~||')[i])
fo1.write("||~||".join(str2)+"\n")
for i in [4, 5, 6, 7, 8, 9, 10, 11]:
str4.append(data.split('||~||')[i])
fo2.write("||~||".join(str4)+"\n")
fo1.close()
fo2.close()
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 30 18:05:35 2020
@author: fengzi
"""
import os
import pandas as pd
from datetime import datetime
def main():
source_dir = 'E:/服管部/满意度调研/满意度影响因子分析/3.18\BI提单数据/BI2020031700005分开/分列测试/集团成员.txt'
target_dir = 'E:/服管部/满意度调研/满意度影响因子分析/3.18\BI提单数据/BI2020031700005分开/分列测试/集团成员.xlsx'
new_colums = "look\r\n"
start_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print("开始时间:",start_time)
#文件开头增加新的一列内容作为列名(原列名太长/不可用)
f = open(source_dir, 'r')
content = f.read() # 读取文件内容
f_new = open('b.txt', 'w')
f_new.write(new_colums) # 开头写入内容并换行
f_new.write(content) # 写入原文件内容
f_list=list(set(f.readlines())) #先把内容readlines()为列表,然后用set集合去重后再转化为列表,赋值于变量f_list
for i in f_list: #for循环列表f_list,判断是否有“\n”字符,如果有,将元素‘\n'移除
if i=='\n':
f_list.remove(i)
f_new.writelines(f_list) #将列表f_list的内容(此时列表已去除换行空白行),通过writelines的方式写入新文件,
f.close()
f_new.close()
os.remove(source_dir) # 移除老文件
os.rename('b.txt', source_dir) # 新文件命名为老文件名
data = pd.read_csv(source_dir,"rb",engine='python') #读入数据出现乱码可添加engine='python'
#字符串切分后结果分列展示
df = pd.DataFrame(data, columns=["look"]) #需要分列的列名
df=df["look"].str.split('', expand=True) # 分列的字符,split默认输出list,设置expand=True结果会分列展示
#print(df)
df.to_excel(target_dir) #列表df存储至excel
end_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print("完成时间",end_time)
if __name__ == "__main__":
main()