# 法1:pandas.read_csv(path,engine='python')
#之所以要加engine='python' 是因为路径里有中文,详情参考文末参考文章
path_folder = ""
path_output = "“”
df_all = pd.DataFrame()
for paths in os.walk(path_folder):
for path in paths[2]:
filepath = path_folder + '\\'+str(path)
df = pd.read_csv(filepath,sep='\t',header=None,engine='python')
df_all = df_all.append(df)
columns =['A','B','C','D']
df_all.columns= columns
df_all.to_excel(path_output,index=False)
# 法2:
classes_path = ""
with open(classes_path) as f:
text_ori = f.readlines()# 全部读取最后返回一个列表存所有的类,每行后面都会带有“\n”
print(type(text_ori))
print(text_ori[0])
# print(text)
f.close()
# 遍历,去除首尾的空白(包括'\n', '\r', '\t', ' '),并按照'\t'分割
# 因为读出来的行是这样的 1 8 8 20190101,中间的空格是\t 制表符
text = [line.strip().split('\t') for line in text_ori]
#若需要连接 则直接join
# text2= ["".join(line.strip().split('\t')) for line in text_ori]
columns =['a','b','c','d']
df_all_2 = pd.DataFrame(text,columns=columns)
df_all2.to_excel(path_output,index=False)
参考:
pandas.read_csv() 报错 OSError: Initializing from file failed