为了提高工作效率(偷懒),用python去解决。
工作需要,需要将excel文件转化为csv文件,要是手工的一个个去转换,每个sheet页不但有几十个字段,中间还夹杂着空格,然后按顺序转换成csv文件,这要是手工去完成,一两个还行,多了那就是要死人的节奏啊,这时候想到了python,走起。
此代码作用:将 excel 表格内容转化为 csv文件
excel文件转化后的样式:35|36|37|38|39|40| |1| | | |0|0|0| |12| | |2|13714341| | | |0460?08???000000001003022|153
1 __author__ = 'zxl' 2 3 import pandas as pd 4 import os 5 import xlrd 6 7 path = r'/Users/zhangxl/Desktop/' 8 filename = 'zxl.xlsx' 9 data = xlrd.open_workbook(os.path.join(path, filename)) 10 sheet_names = data.sheet_names() 11 12 13 for sheet in sheet_names[0:]: 14 df = pd.read_excel(os.path.join(path, filename), 15 sheet_name=sheet, 16 header=None, 17 skiprows=1, #从excel表格第二行读取 18 encoding='utf-8') 19 for i in range(1, df.shape[1] + 1, 2): 20 df1 = df[i] 21 df1.fillna('', inplace=True) 22 df1_str = '|'.join(map(str, df1.to_list())) 23 with open(os.path.join(path, sheet + '_' + str(i) + '.csv'), 24 'w', 25 encoding='utf8') as f: 26 f.write(df1_str)