decode_imported_csv.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # NOT USED ANYWHERE, YOU CAN DELETE THIS IF YOU KNOW WHAT ARE YOU DOING
  2. import os
  3. import csv
  4. import urllib.parse
  5. # Функция для декодирования percent-encoded строки
  6. def decode_percent_encoded_string(encoded_string):
  7. return urllib.parse.unquote(encoded_string)
  8. # Функция для декодирования CSV файла
  9. def decode_csv(file_path):
  10. with open(file_path, mode='r', encoding='utf-8') as infile, open(file_path + '.decoded', mode='w', encoding='utf-8', newline='') as outfile:
  11. reader = csv.reader(infile)
  12. writer = csv.writer(outfile,escapechar='\\', quotechar='"', quoting=csv.QUOTE_ALL, doublequote=True)
  13. for row in reader:
  14. decoded_row = [decode_percent_encoded_string(cell) for cell in row]
  15. writer.writerow(decoded_row)
  16. # Функция для обработки всех CSV файлов в директории
  17. def decode_all_csv_files_in_directory(directory_path):
  18. for filename in os.listdir(directory_path):
  19. if filename.endswith('.csv'):
  20. file_path = os.path.join(directory_path, filename)
  21. print(f"Processing file: {file_path}")
  22. decode_csv(file_path)
  23. os.replace(file_path + '.decoded', file_path)
  24. def main():
  25. directory_path = 'place_your_path_here'
  26. decode_all_csv_files_in_directory(directory_path)
  27. if __name__ == "__main__":
  28. main()