gpt4 book ai didi

python - 我正在尝试使用 Python 将文本文件转换为 CSV

转载 作者:行者123 更新时间:2023-12-01 08:00:33 25 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 文件(如您所见,只能采用字段的多个值之一,并且有一些没有员工姓名的数据需要在输出 CSV 文件中排除):

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

**我尝试了各种方法,但我无法做到(我对编程还很陌生):

使用其他人的例子,我已经尝试过这个,但我认为我的问题需要不同的方法:

records = """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"""

for record in records.split('Employee Name'):
fields = record.split('\n')
Employee_Name = "NA"
Designation = "NA"
ContactNo = "NA"
Qualification = "NA"
Specialization = "NA"
for field in fields:
field_name, field_value = field.split(':')
if field_name == "": # This is employee name, since we split on it
Employee_Name = field_value
if field_name == "Designation":
Designation = field_value
if field_name == "ContactNo":
ContactNo = field_value
if field_name == "Qualification":
Qualification = field_value
if field_name == "Specialization":
Specialization = field_value

这是我的第一个问题,所以请忽略问题中的任何格式错误(如果有不正确的地方,请不要保留问题,我会立即更新)

最佳答案

如果您在代码中的不同位置添加 print 语句,您会发现有时是 record='',有时是 field=''

添加几行:

for record in records.split('Employee Name'):
if record == '':
continue
fields = record.split('\n')

for field in fields:
if field == '':
continue
field_name, field_value = field.split(':')

现在应该可以成功运行。

关于python - 我正在尝试使用 Python 将文本文件转换为 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55752360/

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