当前位置:   article > 正文

将多个csv文件合成一个csv_matlab 合并csv

matlab 合并csv

1.python的writer做法

  1. import csv
  2. import pandas as pd
  3. import os
  4. from statistics import mean
  5. #对dev测试生成的多个csv进行融合
  6. def merge_different_csv():
  7. CSV_C0_NAME = 'file_name'
  8. CSV_C1_NAME = 'real_length'
  9. CSV_C2_NAME = 'dev_length'
  10. CSV_C3_NAME = 'dev_length_abs_error'
  11. CSV_C4_NAME = 'dev_length_relative_error'
  12. CSV_C5_NAME = 'prod_length'
  13. CSV_C6_NAME = 'prod_length_abs_error'
  14. CSV_C7_NAME = 'prod_length_relative_error'
  15. CSV_C8_NAME = 'real_weight'
  16. CSV_C9_NAME = 'dev_weight'
  17. CSV_C10_NAME = 'dev_weight_abs_error'
  18. CSV_C11_NAME = 'dev_weight_relative_error'
  19. CSV_C12_NAME = 'prod_weight'
  20. CSV_C13_NAME = 'prod_weight_abs_error'
  21. CSV_C14_NAME = 'prod_weight_relative_error'
  22. CSV_C15_NAME = 'dev_time'
  23. CSV_C16_NAME = 'prod_time'
  24. csv_path='./data/csv_data'
  25. every_csv__path_list_names=[os.path.join(csv_path,i) for i in os.listdir(csv_path)]
  26. dev_length_error=[]
  27. prod_length_error = []
  28. dev_weight_error = []
  29. prod_weight_error = []
  30. # 计算mean
  31. for every_csv_path in every_csv__path_list_names:
  32. with open(every_csv_path, "r") as csvfile:
  33. reader2 = csv.reader(csvfile) # 读取csv文件,返回的是迭代类型
  34. # print(reader2)
  35. for i,csv_list in enumerate(reader2):
  36. # print(csv_list)
  37. if i:
  38. csv_rows ={CSV_C0_NAME: csv_list[0],
  39. CSV_C1_NAME: csv_list[1],
  40. CSV_C2_NAME: csv_list[2],
  41. CSV_C3_NAME: csv_list[3],
  42. CSV_C4_NAME: csv_list[4],
  43. CSV_C5_NAME: csv_list[5],
  44. CSV_C6_NAME: csv_list[6],
  45. CSV_C7_NAME: csv_list[7],
  46. CSV_C8_NAME: csv_list[8],
  47. CSV_C9_NAME: csv_list[9],
  48. CSV_C10_NAME: csv_list[10],
  49. CSV_C11_NAME: csv_list[11],
  50. CSV_C12_NAME: csv_list[12],
  51. CSV_C13_NAME: csv_list[13],
  52. CSV_C14_NAME: csv_list[14],
  53. CSV_C15_NAME: csv_list[15],
  54. CSV_C16_NAME: csv_list[16]}
  55. dev_length_error.append(float(csv_rows.get(CSV_C4_NAME)))
  56. prod_length_error.append(float(csv_rows.get(CSV_C7_NAME)))
  57. dev_weight_error.append(float(csv_rows.get(CSV_C11_NAME)))
  58. prod_weight_error.append(float(csv_rows.get(CSV_C14_NAME)))
  59. # print(round(mean(dev_length_error),2))
  60. # print(round(mean(prod_length_error), 2))
  61. # print(round(mean(dev_weight_error), 2))
  62. # print(round(mean(prod_weight_error), 2))
  63. CSV_C4_NAME = CSV_C4_NAME + '=' + str(round(mean(dev_length_error), 2)) + '%'
  64. CSV_C7_NAME = CSV_C7_NAME + '=' + str(round(mean(prod_length_error), 2)) + '%'
  65. CSV_C11_NAME = CSV_C11_NAME + '=' + str(round(mean(dev_weight_error), 2)) + '%'
  66. CSV_C14_NAME = CSV_C14_NAME + '=' + str(round(mean(prod_weight_error), 2)) + '%'
  67. print(CSV_C4_NAME, CSV_C7_NAME, CSV_C11_NAME, CSV_C14_NAME)
  68. fieldnames_l = [CSV_C0_NAME, CSV_C1_NAME, CSV_C2_NAME, CSV_C3_NAME, CSV_C4_NAME, CSV_C5_NAME, CSV_C6_NAME,
  69. CSV_C7_NAME, CSV_C8_NAME, CSV_C9_NAME, CSV_C10_NAME, CSV_C11_NAME, CSV_C12_NAME, CSV_C13_NAME,
  70. CSV_C14_NAME, CSV_C15_NAME, CSV_C16_NAME]
  71. csvfile_all = open('./data/pig_dev_prod_data.csv', 'w')
  72. writer_pig_data = csv.DictWriter(csvfile_all, fieldnames=fieldnames_l)
  73. writer_pig_data.writeheader()
  74. for every_csv_path in every_csv__path_list_names:
  75. with open(every_csv_path, "r") as csvfile:
  76. reader2 = csv.reader(csvfile) # 读取csv文件,返回的是迭代类型
  77. # print(reader2)
  78. for i,csv_list in enumerate(reader2):
  79. # print(csv_list)
  80. if i:
  81. csv_rows ={CSV_C0_NAME: csv_list[0],
  82. CSV_C1_NAME: csv_list[1],
  83. CSV_C2_NAME: csv_list[2],
  84. CSV_C3_NAME: csv_list[3],
  85. CSV_C4_NAME: csv_list[4],
  86. CSV_C5_NAME: csv_list[5],
  87. CSV_C6_NAME: csv_list[6],
  88. CSV_C7_NAME: csv_list[7],
  89. CSV_C8_NAME: csv_list[8],
  90. CSV_C9_NAME: csv_list[9],
  91. CSV_C10_NAME: csv_list[10],
  92. CSV_C11_NAME: csv_list[11],
  93. CSV_C12_NAME: csv_list[12],
  94. CSV_C13_NAME: csv_list[13],
  95. CSV_C14_NAME: csv_list[14],
  96. CSV_C15_NAME: csv_list[15],
  97. CSV_C16_NAME: csv_list[16]}
  98. writer_pig_data.writerow(csv_rows)

2.pandas做法

  1. import os
  2. import pandas as pd
  3. #对dev测试生成的多个csv进行融合
  4. def merge_different_csv():
  5. """notice"""
  6. out_csv_path='pig_dev_prod_data.csv'
  7. csv_path = './out_csv'
  8. every_csv_path_list_names=[os.path.join(csv_path,i) for i in os.listdir(csv_path)]
  9. df=pd.read_csv(every_csv_path_list_names[-1])
  10. df.to_csv(out_csv_path, sep=',', encoding='utf-8', index=False)
  11. for i in range(len(every_csv_path_list_names)-1):
  12. df = pd.read_csv(every_csv_path_list_names[i])
  13. df.to_csv(out_csv_path,encoding='utf-8',index=False,header=False, mode='a+')
  14. df_all=pd.read_csv(out_csv_path)
  15. dev_length_relative_error=df_all['dev_length_relative_error']
  16. prod_length_relative_error=df_all['prod_length_relative_error']
  17. real_length=df_all['real_length']
  18. mean_dev_length=round(sum(dev_length_relative_error) / sum(real_length) * 100,2)
  19. mean_prod_length=round(sum(prod_length_relative_error) / sum(real_length) * 100,2)
  20. dev_weight_relative_error = df_all['dev_weight_relative_error']
  21. prod_weight_relative_error = df_all['prod_weight_relative_error']
  22. real_weight = df_all['real_weight']
  23. mean_dev_weight=round(sum(dev_weight_relative_error)/sum(real_weight)*100,2)
  24. mean_prod_weight=round(sum(prod_weight_relative_error) / sum(real_weight) * 100,2)
  25. print('测试模型长度误差 %.2f, 生产模型长度误差 %.2f' % (mean_dev_length, mean_prod_length))
  26. print('测试模型重量误差 %.2f, 生产模型重量误差 %.2f' % (mean_dev_weight, mean_prod_weight))
  27. dev_length_error_col_name = 'dev_length_relative_error {0}%'.format(mean_dev_length)
  28. prod_length_error_col_name = 'prod_length_relative_error {0}%'.format(mean_prod_length)
  29. dev_weight_error_col_name = 'dev_weight_relative_error {0}%'.format(mean_dev_weight)
  30. prod_weight_error_col_name = 'prod_weight_relative_error {0}%'.format(mean_prod_weight)
  31. df_all.rename(columns={'dev_length_relative_error': dev_length_error_col_name,
  32. 'prod_length_relative_error': prod_length_error_col_name,
  33. 'dev_weight_relative_error': dev_weight_error_col_name,
  34. 'prod_weight_relative_error': prod_weight_error_col_name
  35. },inplace=True)
  36. df_all.to_csv(out_csv_path, encoding='utf-8',index=False)
  37. if __name__ == '__main__':
  38. merge_different_csv()

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/940708
推荐阅读
相关标签
  

闽ICP备14008679号