Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import os
- files_directory = '/Users/pavel/Desktop/Work/Python/files/semrush_combiner' # путь к папке с файлами
- today = time.strftime('%Y%m%d')
- now = time.strftime('%H%M%S')
- combine_file_name = '/Users/pavel/Desktop/Work/Python/files/' + 'semrush_files_combine' + '_' + today + '_' + now + '.csv'
- files = os.listdir(files_directory) # получаем список файлов в папке
- combine_file = open(combine_file_name, 'a')
- combine_file.write('Keyword;Volume;Keyword Difficulty\n')
- keywords = []
- for export_file in files:
- if export_file == '.DS_Store': # для MacOS
- continue
- f = open(files_directory + '/' + export_file, 'r')
- while True: # перебираем до тех пор, пока в строке есть текст
- line = f.readline()
- line = line.split(';')
- if line == ['']:
- break
- strline = str(line)
- if strline.startswith("['Keyword', 'Volume', 'Keyword Difficulty'"): # пропускаем шапку
- continue
- elif {line[0] : {'Volume': line[1], 'Keyword Difficulty' : line[2]}} in keywords: # проверяем есть ли дубль
- print(f'Удален дубль {line[0]}')
- continue
- else:
- keywords.append({line[0] : {'Volume': line[1], 'Keyword Difficulty' : line[2]}})
- combine_file.write(f'{line[0]};{line[1]};{line[2]}\n')
- f.close()
- combine_file.close()
Add Comment
Please, Sign In to add comment