[Solved] Search in text file and save in Excel


It’s quite easy using pandas and a dict:

with open('file.txt', 'r') as f:
    lines = f.readlines()

students = []
student = {}
for line in lines:
    if ':' in line:
        student['id'] = line.split(':')[0]
    elif 'name' in line:
        student['Name'] = line.split('=')[1].replace('\n','')
    elif 'Age' in line:
        student['Age'] = line.split('=')[1].replace('\n','')
    elif 'Grade' in line:
        student['Grade'] = line.split('=')[1].replace('\n','')
        students.append(student)
        print(student)
        student = {}
    
import pandas as pd
df = pd.DataFrame(students)
df.to_excel("output.xlsx")
print(df)

0

solved Search in text file and save in Excel