一、大量文件名读取与入表

  • 业务需求:把207张Excel表的文件名与表中的某个字段数据合成一张表
  • 步骤:

    1. 首先,通过递归把207张表的文件名遍历出来
    2. 把遍历出来的文件名存储到列表中
    3. 通过Pandas模块中的read_excel()函数把对应的字段存储到列表中
    4. 难点:解决一个文件名对应多条数据,此时该文件名重复的问题?

用Python做非结构化数据清洗 - 图1

代码实现如下:

  1. import os
  2. import pandas
  3. import pandas as pd
  4. from pandas import read_excel
  5. def show_files(path, all_files):
  6. # 首先遍历当前目录所有文件及文件夹
  7. file_list = os.listdir(path)
  8. # 准备循环判断每个元素是否是文件夹还是文件,是文件的话,把名称传入list,是文件夹的话,递归
  9. for file in file_list:
  10. # 利用os.path.join()方法取得路径全名,并存入cur_path变量,否则每次只能遍历一层目录
  11. cur_path = os.path.join(path, file)
  12. # 判断是否是文件夹
  13. if os.path.isdir(cur_path):
  14. show_files(cur_path, all_files)
  15. else:
  16. all_files.append(file)
  17. return all_files
  18. """
  19. oldpath:文件的源路径;
  20. new_file_name:生成的最新的文件;
  21. """
  22. def to_excel_num(oldpath,new_file_name):
  23. contents = show_files(oldpath, [])#所有文件的文件名列表;
  24. list_num = []
  25. new_df = []
  26. new_df2 = []
  27. # 循环打印show_files函数返回的文件名列表
  28. for content in contents:
  29. df = read_excel(str(oldpath)+str(content))
  30. for k in range(0,int(len(df))):
  31. list_num.append(content.split('.')[0])
  32. for v in df.values.tolist():
  33. new_df.append(v[1])
  34. new_df2.append(v[1])
  35. df = pd.DataFrame({
  36. "文件名": list_num,
  37. "number":new_df2,
  38. # '好友ID': new_df
  39. })
  40. write = pd.ExcelWriter(new_file_name)
  41. df.to_excel(write,index=False)
  42. write.save()
  43. oldpath = "./files/"
  44. new_file_name = "./new_file_two.xlsx"
  45. to_excel_num(oldpath, new_file_name)

二、Excel单元格复杂数据清洗

  • 业务需求:把1张Excel表的json格式字段,分解成有规则的数据,并生成一张新的Excel表
  • 步骤:

    1. 首选,获取到json格式字段的列值存于列表中
    2. 去掉两边的{与},并以逗号隔开
    3. 留下表中的二个字段,并写入新的Excel表中
    4. 难点:解决一个字段对应多条数据,此时字段重复的问题?

原表:

用Python做非结构化数据清洗 - 图2

实现效果:

用Python做非结构化数据清洗 - 图3

代码实现如下:

  1. from pandas import read_excel
  2. import pandas as pd
  3. """
  4. file_name参数的值表示要修改的文件;
  5. new_file_name参数的值表示生成的新文件地址
  6. """
  7. def test_ip(file_name,new_file_name):
  8. df = read_excel(file_name)
  9. list_users = df['USERNAME'].values.tolist() #获取到USERS列的值;
  10. list_ip = df['IP'].values.tolist() #获取到IP列的值;
  11. list_users_len = [] #装users列的长度,方便后面重复;
  12. new_list_users = [] #把所有的值放到这;
  13. for v in list_users:
  14. vv = v.lstrip("{")
  15. vv = vv.rstrip("}")
  16. new_list = vv.split(",")
  17. list_users_len.append(len(new_list))
  18. for k in new_list:
  19. new_list_users.append(k)
  20. #开始对ip值重复的数量进行一一对应;
  21. new_list_ip = []
  22. len_ip = len(list_ip)
  23. for k in range(0,len_ip):
  24. for kk in range(0,list_users_len[k]):
  25. new_list_ip.append(list_ip[k])
  26. #装入数据框中;
  27. new_df = pd.DataFrame({
  28. "IP": new_list_ip,
  29. "USERNAME":new_list_users
  30. })
  31. write = pd.ExcelWriter(new_file_name)
  32. new_df.to_excel(write,index=False)
  33. write.save()
  34. #下面是调用以上函数的参数
  35. file_name = "./meiya_02.xlsx"
  36. new_file_name = "./new_file_two.xlsx"
  37. test_ip(file_name,new_file_name)