简体   繁体   English

在文本文件中搜索并保存在 Excel

[英]Search in text file and save in Excel

I have a text file with information about 1000 student So i need to save each student details in an excel sheet我有一个包含 1000 名学生信息的文本文件所以我需要将每个学生的详细信息保存在 excel 表中

Heres a sample of the data:以下是数据示例:

0000:
     name=Jack
     Age=16
     Grade=90
0001:
     name=Max
     Age=18
     Grade=85
0002:
     name=Kayle
     Age=17
     Grade=92

I want to have a result like this:我想要这样的结果:

在此处输入图像描述

It's quite easy using pandas and a dict:使用 pandas 和字典非常容易:

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)

I always use Word for such a job.我总是使用 Word 来完成这样的工作。 With Replace, search for Paragraph Marks and replace them with a Tab-character.使用替换,搜索段落标记并将它们替换为制表符。

Eg replace :[paragraph mark][space][space][space][space]name= with a [tab character] .例如,将:[paragraph mark][space][space][space][space]name=替换为[tab character] With that you get rid of all the rubbish and you end up with 0000[tab character]Jack .这样,您就可以摆脱所有垃圾,最终得到0000[tab character]Jack

When you're done with all lines of tab separated data, select all the lines with data (make sure not to select empty lines without the three tab-characters, otherwise it won't work) and click on Insert -> Table -> Insert Table... Now the data is converted into a Word table.完成所有制表符分隔数据行后, select 所有数据行(确保不要 select 没有三个制表符字符的空行,否则它将不起作用)并单击插入->表格->插入表格... 现在数据被转换为 Word 表格。 Just copy the table to Excel and you're done.只需将表复制到 Excel 即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM