fileio.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import csv
  2. import os
  3. import json
  4. # TODO 增加文件传输的方式(文件夹、文件、数据流)
  5. import re
  6. import xlrd
  7. import xlwt
  8. def write_csv(node_list):
  9. with open("tmp_res.csv", "w", newline="") as file:
  10. csvwriter = csv.writer(file, dialect=("excel"))
  11. # csv文件插入一行数据,把下面列表中的每一项放入一个单元格(可以用循环插入多行)
  12. csvwriter.writerows(node_list)
  13. def walk_files(path, endpoint='.py'):
  14. file_list = []
  15. for root, dirs, files in os.walk(path):
  16. for file in files:
  17. file_path = os.path.join(root, file)
  18. if file_path.endswith(endpoint):
  19. file_list.append(open(file_path, encoding='utf-8'))
  20. return file_list
  21. def walk_files_path(path, endpoint='.py'):
  22. file_list = []
  23. for root, dirs, files in os.walk(path):
  24. for file in files:
  25. file_path = os.path.join(root, file)
  26. if file_path.endswith(endpoint):
  27. file_list.append(file_path)
  28. return file_list
  29. def load_json(json_file):
  30. with open(json_file, 'r') as load_f:
  31. load_dict = json.load(load_f)
  32. return load_dict
  33. def write_json(json_file, data):
  34. with open(json_file, 'w') as file:
  35. file.write(json.dumps(data))
  36. def load_location(file_path):
  37. wk_bk = xlrd.open_workbook(file_path)
  38. wk_all = wk_bk.sheet_by_index(0)
  39. location_list = [(location.split('/')[0].replace('.py', '').replace('.', '/') + '.py', int(location.split('/')[1]))
  40. for location in list(wk_all.col_values(0))[2:] if location != '']
  41. datatype_list = [data_type.split('/')[-1] for data_type in wk_all.col_values(1)[2:] if data_type != '']
  42. purpose_list = [purpose for purpose in wk_all.col_values(2)[2:] if purpose != '']
  43. location_dict = {}
  44. for i in range(len(location_list)):
  45. if location_list[i] in location_dict.keys():
  46. location_dict[location_list[i]].append((datatype_list[i], purpose_list[i]))
  47. else:
  48. location_dict[location_list[i]] = [(datatype_list[i], purpose_list[i])]
  49. # print(len(location_dict.items()))
  50. # for item, value in location_dict.items():
  51. # print(item, value)
  52. # print(location_dict)
  53. return
  54. def write_to_excel(stamp, file_path):
  55. book = xlwt.Workbook(encoding='utf-8')
  56. sheet_datatype = book.add_sheet("DataType")
  57. sheet_purpose = book.add_sheet("Purpose")
  58. sheet_datatype.write(0, 0, "Location")
  59. sheet_datatype.write(0, 1, "DataType")
  60. sheet_datatype.write(0, 2, "confidence")
  61. sheet_datatype.write(0, 3, "Script")
  62. sheet_purpose.write(0, 0, "Location")
  63. sheet_purpose.write(0, 1, "Purpose")
  64. sheet_purpose.write(0, 2, "confidence")
  65. sheet_purpose.write(0, 3, "Script")
  66. i = 1
  67. j = 1
  68. for st in stamp:
  69. loc = st[0] + ' ' + str(st[1])
  70. datatype = list(set([data[0] for data in st[2] if data[0] != 'Data']))
  71. purpose = list(set([data[1] for data in st[2] if data[1] != 'Usage']))
  72. for data in datatype:
  73. sheet_datatype.write(i, 0, loc)
  74. sheet_datatype.write(i, 1, data)
  75. sheet_datatype.write(i, 2, 1)
  76. i += 1
  77. for pur in purpose:
  78. sheet_purpose.write(j, 0, loc)
  79. sheet_purpose.write(j, 1, pur)
  80. sheet_purpose.write(j, 2, 1.0 / len(purpose))
  81. j += 1
  82. book.save(file_path)
  83. def list_to_excel(file_path, data_type_list, purpose_list):
  84. book = xlwt.Workbook(encoding='utf-8')
  85. sheet_datatype = book.add_sheet("DataType")
  86. sheet_purpose = book.add_sheet("Purpose")
  87. sheet_datatype.write(0, 0, "Location")
  88. sheet_datatype.write(0, 1, "DataType")
  89. sheet_datatype.write(0, 2, "confidence")
  90. sheet_datatype.write(0, 3, "Script")
  91. sheet_purpose.write(0, 0, "Location")
  92. sheet_purpose.write(0, 1, "Purpose")
  93. sheet_purpose.write(0, 2, "confidence")
  94. sheet_purpose.write(0, 3, "Script")
  95. i = 1
  96. j = 1
  97. for data in data_type_list:
  98. sheet_datatype.write(i, 0, data[0])
  99. sheet_datatype.write(i, 1, data[1])
  100. sheet_datatype.write(i, 2, 1)
  101. i += 1
  102. for pur in purpose_list:
  103. sheet_purpose.write(j, 0, pur[0])
  104. sheet_purpose.write(j, 1, pur[1])
  105. sheet_purpose.write(j, 2, 1)
  106. j += 1
  107. book.save(file_path)
  108. def load_data_purpose_split(file_path):
  109. wk_bk = xlrd.open_workbook(file_path)
  110. wk_all = wk_bk.sheet_by_index(0)
  111. data_type_list = []
  112. purpose_list = []
  113. for i in range(2, wk_all.nrows):
  114. path = wk_all.row_values(i)[0]
  115. data_type = wk_all.row_values(i)[1]
  116. purpose = wk_all.row_values(i)[2]
  117. if path != '':
  118. path = path.replace('/', ' ').replace('.', '/').replace('/py', '.py')
  119. if data_type != '' and data_type != 'any':
  120. data_type_list.append((path, data_type.split('/')[-1]))
  121. if purpose != 'Usage' and purpose != '':
  122. purpose_list.append((path, purpose))
  123. return data_type_list, purpose_list
  124. def verify_file_list(file_list):
  125. for file in file_list:
  126. with open(file, encoding='utf-8') as f:
  127. content = f.read()
  128. if re.search(r'=[ ]*lambda[^:]*:', content):
  129. content_new = re.sub(r'=[ ]*lambda[^:]*:', '=', content)
  130. file_new = file.replace('.py', '-backup.py')
  131. with open(file_new, 'w', encoding='utf-8') as f_new:
  132. f_new.write(content_new)
  133. file_list.remove(file)
  134. file_list.append(file_new)
  135. return file_list
  136. if __name__ == '__main__':
  137. file_list = walk_files_path('/Users/liufan/program/PYTHON/SAP/TestProject')
  138. verify_file_list(file_list)