简体   繁体   中英

How to write the next line in a text file to a new text file

I'm trying to sort data in a text-file by using XML tags. I know about all the libraries but that's not the way I want to go about doing it. If the file I have is:

UIHC
2
A31415
54
M
180
98
6.7
100
No
130
65
A32545
62
F
160
80
7.2
120
Yes
180
92

I need the output to look like:

<patient>       
<patientID>A31415</patientID>   
<age>54</age>   
<gender>M</gender>  
<height>180</height>    
<weight>90</weight> 
<hba1c>6.7</hba1c>  
<cholesterol>100</cholesterol>  
<smoker>No<smoker>  
<systolic>130</systolic>    
<diastolic>65</diastolic>   
</patient>  
<patient>       
<patientID>A32545</patientID>       
<age>62</age>   
<gender>F</gender>  
<height>160</height>    
<weight>80</weight> 
<hba1c>7.2</hba1c>  
<cholesterol>120</cholesterol>  
<smoker>Yes<smoker> 
<systolic>180</systolic>    
<diastolic> 92</diastolic>  
</patient>

My code is:

def codeData(filename):
    newFile = filename
    newFile = newFile.replace(".txt", "")
    newFile = str(newFile) + "XML.txt"    
    originalFile = open(filename,'r')    
    firstLine  = originalFile.readline()
    secondLine = originalFile.readline()
    original = originalFile.readlines()
    index = 0
    file = open(newFile, 'w')
    for line in original:
        index = index + 1
        if index%11 == 1:
            file.write('<patientID>'+str(original[0]).strip('\n')+'</patientID>\n')
        if index%11 == 2:
            file.write('<age>'+str(original[1]).strip('\n')+'</age>\n')
        if index%11 == 3:
            file.write('<gender>'+str(original[2]).strip('\n')+'</gender>\n')
        if index%11 == 4:
            file.write('<height>'+str(original[3]).strip('\n')+'</height>\n')
        if index%11 == 5:
            file.write('<weight>'+str(original[4]).strip('\n')+'</weight>\n')
        if index%11 == 6:
            file.write('<HBA1C>'+str(original[5]).strip('\n')+'</HBA1C>\n')
        if index%11 == 7:
            file.write('<cholesterol>'+str(original[6]).strip('\n')+'</cholesterol>\n')
        if index%11 == 8:
            file.write('<smoker>'+str(original[7]).strip('\n')+'</smoker>\n')
        if index%11 == 9:
            file.write('<systolic>'+str(original[8]).strip('\n')+'</systolic>\n')
        if index%11 == 10:
            file.write('<diastolic>'+str(original[9]).strip('\n')+'</diastolic>\n')

But with this code my output repeats only one of the patients. I know it's because I'm specifying to write a specific line. My output is:

<patientID>A31415</patientID>
<age>54</age>
<gender>M</gender>
<height>180</height>
<weight>98</weight>
<HBA1C>6.7</HBA1C>
<cholesterol>100</cholesterol>
<smoker>No</smoker>
<systolic>130</systolic>
<diastolic>65</diastolic>
<patientID>A31415</patientID>
<age>54</age>
<gender>M</gender>
<height>180</height>
<weight>98</weight>
<HBA1C>6.7</HBA1C>
<cholesterol>100</cholesterol>
<smoker>No</smoker>
<systolic>130</systolic>

So my question is how do I write the next line in the file, instead of repeating. Any help would be appreciated. And yes, all the information is completely made up.

  1. Use for index, line in enumerate(original) to iterate over your input without having to keep track of the index yourself.

  2. Access the current line the for-loop gives you instead of using (and especially hardcoding) indices like original[0] .

  3. Once everything works like you want it to, you might want to consider using a list or dictionary of XML tags instead of the long list of if s you're using now.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM