gpt4 book ai didi

python - 将结构化文本文件转换为 csv(无法将行更改为列):

转载 作者:太空宇宙 更新时间:2023-11-04 09:30:57 24 4
gpt4 key购买 nike

我正在尝试用 Python 将文本文件转换为 CSV输入的文本文件如下:

Employee Name: Dr.john doe
Designation: Professor
Email: johndoe@google.com
ContactNo: 1234567, 9999999
Qualification: M.Tech., Ph.D.
Area of Interest / Specialisation: network security
Employee Name: Dr. john doe2
Designation: Professor2
Email: johndoe2@google.com
ContactNo: 222222222
Qualification: B.Tech., Ph.D.
Area of Interest / Specialisation: network security2
Employee Name: Dr. john doe3
Designation: Associate Professor3
Email: johndoe3@google.com
ContactNo: 333333,4444444
Qualification: Ph.D.
Area of Interest / Specialisation: network security3
Designation: Associate Professor4
Email: johndoe4@google.com
ContactNo: 44444444 ,Intercom No.44444
Qualification: : M.Sc.
Designation: Programmer
Email: johndoe5@google.com
ContactNo: 5555555555 ,Intercom No.5555
Qualification: Ph.D |Computer Science
Designation: Computer Operator
Email: johndoe6@google.com
ContactNo: 666666666
Qualification: D.C.Sc. & E.,
Designation: Computer Operator
Email: johndoe7@google.com
ContactNo: 777777777 ,Intercom No.77777<
Qualification: D.E & TC.,
Designation: Instructor4
Email: johndoe8@google.com
ContactNo: 8888888888 ,Intercom No.8888
Qualification: D.C.Sc. & E.,`

我需要以下格式的 CSV 格式

Employee name,designation,email,contact,Qualification,Specialisation       
Dr. john doe,Professor,johndoe@google.com,1234567,B.E.,network security
Dr. john doe2,Professor,johndoe2@google.com,222222222,M.S.,network security2
Dr. john doe3,Associate,Professor3,johndoe3@gmail.com,333333,M.Tech.,network security3

我试过了

with open('test.txt', 'r') as records:
stripped = (line.strip() for line in records)
lines = (line.split(":") for line in stripped if line)
with open('log.csv', 'w') as out_file:
writer = csv.writer(out_file)
writer.writerows(lines)

我上面的代码给出了以下只有两行的输出(我不知道如何制作 6 列并在行中添加元组):

Employee Name, Dr.john doe
Designation, Professor
Email, johndoe@google.com
ContactNo, 1234567, 9999999
Qualification, M.Tech., Ph.D.
Area of Interest / Specialisation, network security
Employee Name, Dr. john doe2
Designation, Professor2
Email, johndoe2@google.com
ContactNo, 222222222
Qualification, B.Tech., Ph.D.
Area of Interest / Specialisation, network security2
Employee Name, Dr. john doe3
Designation, Associate Professor3
Email, johndoe3@google.com
ContactNo, 333333,4444444
Qualification, Ph.D.
Area of Interest / Specialisation, network security3

简而言之:我能够将属性名称及其值分开,但我不知道如何在特定字段中填充值。

最佳答案

如果您熟悉 pandas,那么您可以简单地使用此代码

import pandas as pd

with open('test.txt', 'r') as records:
lines = [(line.split(':'))[1] for line in records.readlines()]
col_titles = ('Employee name', 'designation','email','contact','Qualification','Specialisation')
data = pd.np.array(lines).reshape((len(lines) // 6, 6))
pd.DataFrame(data, columns=col_titles).to_csv("output.csv", index=False)

关于python - 将结构化文本文件转换为 csv(无法将行更改为列):,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55762902/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com