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.
https://stackoverflow.com/questions/67362721/convert-txt-extract-to-csv May 03, 2021 at 10:04AM
没有评论:
发表评论