Hi I would like to extract a table from a txt file and send it to a csv file. I have worked out a script to extract the table but I cannot make it to the correct format.
cc = "March TXT format BRA.txt"
title = ""
i = 0
fields = [
"Gender",
"InPat St",
"Admits",
"Trans In",
"Disch",
"Deaths",
"Tran Out",
"InPat End",
"Leave Days",
"Day Only",
"Births",
"No Babies",
"Baby Days",
"Bed Days",
"Location",
"Location2",
"Date",
]
with open(cc) as data:
lines = data.readlines()[2:27]
title = lines[0].strip()
for line in lines:
line = line.split()
if len(line) == 14:
if line[0] != "Total":
if i < 2:
line += ["Ward: Rehabilitation - A Ward"]
line += ["Braeside Hospital"]
line.append(title)
else:
if i < 4:
line += ["Ward: Palliative Care - B Ward"]
line += ["Braeside Hospital"]
line.append(title)
else:
line += ["Ward: Aged Care Psychiatric - C Ward"]
line += ["Braeside Hospital"]
line.append(title)
i += 1
print(line)
The result is the following:
['Female', '17', '19', '0', '22', '0', '0', '14', '0', '1', '0', '0', '0', '479', 'Ward: Rehabilitation - A Ward', 'Braeside Hospital', 'March 2021']
['Male', '15', '16', '0', '13', '0', '0', '18', '0', '0', '0', '0', '0', '497', 'Ward: Rehabilitation - A Ward', 'Braeside Hospital', 'March 2021']
['Female', '10', '7', '0', '3', '10', '0', '4', '1', '0', '0', '0', '0', '253', 'Ward: Palliative Care - B Ward', 'Braeside Hospital', 'March 2021']
['Male', '7', '17', '0', '3', '13', '0', '8', '2', '0', '0', '0', '0', '211', 'Ward: Palliative Care - B Ward', 'Braeside Hospital', 'March 2021']
['Female', '6', '4', '0', '4', '0', '0', '6', '0', '0', '0', '0', '0', '149', 'Ward: Aged Care Psychiatric - C Ward', 'Braeside Hospital', 'March 2021']
['Male', '9', '5', '0', '5', '0', '0', '9', '0', '0', '0', '0', '0', '304', 'Ward: Aged Care Psychiatric - C Ward', 'Braeside Hospital', 'March 2021']
I would like to add those to csv file and I am struggling to do so.
Thank you for your help.
This was many times on Stackoverflow.
You can convert line to string
row = ','.join(line)
and then you can save in file
f.write(row + '\n')
and you can do it inside your for
-loop
OR if you will keep all lines on list then you could do this at the end
data = [
['Female', '17', '19', '0', '22', '0', '0', '14', '0', '1', '0', '0', '0', '479', 'Ward: Rehabilitation - A Ward', 'Braeside Hospital', 'March 2021'],
['Male', '15', '16', '0', '13', '0', '0', '18', '0', '0', '0', '0', '0', '497', 'Ward: Rehabilitation - A Ward', 'Braeside Hospital', 'March 2021'],
['Female', '10', '7', '0', '3', '10', '0', '4', '1', '0', '0', '0', '0', '253', 'Ward: Palliative Care - B Ward', 'Braeside Hospital', 'March 2021'],
['Male', '7', '17', '0', '3', '13', '0', '8', '2', '0', '0', '0', '0', '211', 'Ward: Palliative Care - B Ward', 'Braeside Hospital', 'March 2021'],
['Female', '6', '4', '0', '4', '0', '0', '6', '0', '0', '0', '0', '0', '149', 'Ward: Aged Care Psychiatric - C Ward', 'Braeside Hospital', 'March 2021'],
['Male', '9', '5', '0', '5', '0', '0', '9', '0', '0', '0', '0', '0', '304', 'Ward: Aged Care Psychiatric - C Ward', 'Braeside Hospital', 'March 2021'],
]
with open('output1.csv', 'w') as f:
for line in data:
f.write(','.join(line) + '\n')
But iw would be safer to use standard module csv
for this - because sometimes text may have comma ,
or new line \n
or other char which may need to use " "
or other method to create safe data.
You can do it in your loop or at the end using writerow
with every `line separately
import csv
data = [ ...lines... ]
with open('output.csv', 'w') as f:
csv_writer = csv.writer(f)
for line in data:
csv_writer.writerow(line)
or using writerows
(with char s
at the end) to write all at once
with open('output.csv', 'w') as f:
csv_writer = csv.writer(f)
csv_writer.writerows(data)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.