Парсинг данных на PythonPython

Программы на Python
Anonymous
Парсинг данных на Python

Сообщение Anonymous »


I have the following txt file from which I want to output 2 dictionaries txt files with the use of data-text parsing method that doesn't use row numbers, dues to the fact that it should be applicable to larger txt files.
  • The "First Chapter ed" dictionary should look something like: "Height1" : { "Fir_ColumnB" : 123.50, "Fir_ColumnC" : 4, "Fir_ColumnD" : 31}, "Height2" : { "Fir_ColumnB" : 2334.00, "Fir_ColumnC" : 62, "Fir_ColumnD" : 0}, "Height3" : {.....}
[*]The "Second Chapter ed" dictionary should include the values under Sec_ColumnA as keys and the values of Sec_ColumnB. Like:
  • {"Row1" : "12251m", "Row2" : "3231m","Row3" : "31412m"}

But I have faced some difficulties due to those back to back "****" rows.
creating_testing_1 TESTING_1 ; TESTING_1 ; TESTING_1 ; First Chapter ed TESTING_1 ; ******************************************************** TESTING_1 ; Fir_ColumnA Fir_ColumnB Fir_ColumnC Fir_ColumnD TESTING_1 ; ******************************************************** TESTING_1 ; Height1 123.50 4 31 TESTING_1 ; Height2 2334.00 62 0 TESTING_1 ; Height3 0.00 23 23 TESTING_1 ; ******************************************************** TESTING_1 ; TESTING_1 ; Second Chapter ed TESTING_1 ; ******************************************************** TESTING_1 ; Sec_ColumnA Sec_ColumnB TESTING_1 ; ******************************************************** TESTING_1 ; Row1 12251m TESTING_1 ; Row2 3231m TESTING_1 ; Row3 31412m TESTING_1 ; ******************************************************** TESTING_1 ; TESTING_1 ; Ending... TESTING_1 ; def parse_txt_file(filename): data = {} current_chapter = None with open(filename, 'r') as file: for line in file: line = line.strip() if line.startswith(';'): continue if 'First Chapter ed' in line: current_chapter = 'First Chapter ed' data[current_chapter] = {} continue elif 'Second Chapter ed' in line: current_chapter = 'Second Chapter ed' data[current_chapter] = {} continue elif 'Ending...' in line: break if current_chapter: if line.startswith('*'): continue columns = line.split() if len(columns) == 4: key = columns[0] values = columns[1:] if current_chapter == 'First Chapter ed': data[current_chapter][key] = { "Fir_ColumnB": float(values[0]), "Fir_ColumnC": int(values[1]), "Fir_ColumnD": int(values[2]) } elif current_chapter == 'Second Chapter ed': data[current_chapter][key] = values[0] return data def save_dictionary_to_txt(data, filename): with open(filename, 'w') as file: for chapter, chapter_data in data.items(): file.write(f"{chapter}\n") for key, value in chapter_data.items(): if isinstance(value, dict): file.write(f'"{key}" : {value}\n') else: file.write(f'"{key}" : "{value}"\n') file.write('\n') def main(): filename = 'short_test_2.txt' data = parse_txt_file(filename) for chapter, chapter_data in data.items(): save_dictionary_to_txt({chapter: chapter_data}, f'{chapter.replace(" ", "_").lower()}.txt') if __name__ == "__main__": main()

Источник: https://stackoverflow.com/questions/780 ... -on-python

Вернуться в «Python»