pavelrev

Semrush Files Combiner

Apr 1st, 2021 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.49 KB | None | 0 0
  1. import time
  2. import os
  3.  
  4.  
  5. files_directory = '/Users/pavel/Desktop/Work/Python/files/semrush_combiner'  # путь к папке с файлами
  6. today = time.strftime('%Y%m%d')
  7. now = time.strftime('%H%M%S')
  8. combine_file_name = '/Users/pavel/Desktop/Work/Python/files/' + 'semrush_files_combine' + '_' + today + '_' + now + '.csv'
  9.  
  10. files = os.listdir(files_directory)  # получаем список файлов в папке
  11.  
  12. combine_file = open(combine_file_name, 'a')
  13. combine_file.write('Keyword;Volume;Keyword Difficulty\n')
  14. keywords = []
  15. for export_file in files:
  16.     if export_file == '.DS_Store':  # для MacOS
  17.         continue
  18.    
  19.     f = open(files_directory + '/' + export_file, 'r')
  20.     while True:  # перебираем до тех пор, пока в строке есть текст
  21.         line = f.readline()
  22.         line = line.split(';')
  23.         if line == ['']:
  24.             break
  25.         strline = str(line)
  26.         if strline.startswith("['Keyword', 'Volume', 'Keyword Difficulty'"):  # пропускаем шапку
  27.             continue
  28.         elif {line[0] : {'Volume': line[1], 'Keyword Difficulty' : line[2]}} in keywords:  # проверяем есть ли дубль
  29.             print(f'Удален дубль {line[0]}')
  30.             continue
  31.         else:
  32.             keywords.append({line[0] : {'Volume': line[1], 'Keyword Difficulty' : line[2]}})
  33.             combine_file.write(f'{line[0]};{line[1]};{line[2]}\n')
  34.     f.close()
  35.  
  36. combine_file.close()
Add Comment
Please, Sign In to add comment