I'd like to list the contents in the text in Python and output them to CSV, but I'm having trouble because I don't know how to do it.
I made a code based on what I looked into, but it's not what I imagined, so I want your help.
Text Files
Name: A
Class: B
Grade: C
Grades: D
Name: AA
classes:BB
Grade: CC
Grades: DD
List the above :
and later in Python as follows:
[A, B, C, D]
[AA, BB, CC, DD]
Finally, I would like to separate ,
and output it to CSV (as shown in the picture below).
I was able to write the Python code as follows, but I would appreciate it if you could tell me how to make it shorter and produce the desired output.
Current State Code:
with open(r"txt file", encoding='utf-8')asf:
lines=f.readlines()
stat = 0
For line in lines:
if stat == 0:
if(line.startswith("Name:"):
print(line.strip("Name:")
stat = 1
For line in lines:
if stat == 1:
if(line.startswith("class")):
print(line.strip("Class:")
stat = 2
For line in lines:
if stat == 2:
if(line.startswith("School Year:"):
print(line.strip("School Year:")
stat = 3
For line in lines:
if stat == 3:
if(line.startswith("Results:"):
print(line.strip("Results:")
import pandas as pd
with open(r'result_sheet.txt', encoding='utf-8') asf:
records = [
dict(j.split(':') for jin i.split('\n') if j)
for i in f.read().replace('\r', ').split('\n\n') if i
]
df = pd.DataFrame(records)
df.to_excel('result_sheet.xlsx', index=False)
import csv
with open(r'result_sheet.txt', encoding='utf-8') asf:
records = [
dict(j.split(':') for jin i.split('\n') if j)
for i in f.read().replace('\r', ').split('\n\n') if i
]
with open('result_sheet.csv', 'w') asf:
writer=csv.DictWriter(f,fieldnames=records[0].keys())
writer.writeheader()
For in records:
writer.writerow(r)
result_sheet.csv
Name, class, grade, grade
A, B, C, D
AA, BB, CC, DD
in environments with pandas and openpyxl installed
Grades: D
Name: AA
In , 成績Results:D 「 and :Name:AA 」 are assumed to be empty lines only.
import pandas as pd
df=pd.read_table(r "txt file", encoding='utf-8', header=None, engine='python', sep=':')
item_num=len(set(df.values[:,0]))
aary=df.values[:,1].reshape(-1,item_num)
df2=pd.DataFrame(ary, columns=df.values[0:item_num,0])
df2.to_excel('data2.xlsx', header=True, index=False)
If you want standard list type data, please list it from the above aary.
lst=list(map(list,ary))
print(lst)
# [['A', 'B', 'C', 'D', ['AA', 'BB', 'CC', 'DD']]
© 2024 OneMinuteCode. All rights reserved.