简体   繁体   English

如何遍历.txt文件中的x行

[英]How to iterate over x amount of lines in a .txt file

I'm trying to write a program to sort and tag lines in a file. 我正在尝试编写一个程序来对文件中的行进行排序和标记。 For example, suppose I have a .txt file of a health clinic with a variety of information about a patient. 例如,假设我有一家健康诊所的.txt文件,其中包含有关患者的各种信息。 I want to tag the information. 我想标记信息。 Suppose the data is given in the following order: 假设数据按以下顺序给出:

Patient ID  
Age 
Gender  
Height  
Weight  
HBA1C level 
Cholesterol 
Smoker status   
Systolic BP 
Diastolic BP

And suppose the file contains the following information (all of which is made up): 并假设文件包含以下信息(所有信息均已构成):

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

My problem is trying to write a loop for each patient, with 我的问题是尝试为每个患者编写一个循环

A31415  
54  
M   
180 
90  
6.7 
100 
No  
130 
65

being one patient and 作为一个病人,

A32545  
62  
F   
160 
80  
7.2 
120 
Yes 
180 
92

being the second. 是第二。 I'm struggling to get the code to produce the following result: 我正在努力获取代码以产生以下结果:

<patient>       
<patientID> A31415  </patientID>    
<clinic>    UIHC    </clinic>   
<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>    
<clinic>    UIHC    </clinic>   
<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>

Any help would be greatly appreciated. 任何帮助将不胜感激。

This seems quite feasible. 这似乎很可行。 I think something like this should work... 我认为这样的事情应该起作用...

file_keys = ['Patient ID', 'Age', 'Gender',  
             'Height', 'Weight', 'HBA1C level' 
             'Cholesterol', 'Smoker status',   
             'Systolic BP', 'Diastolic BP']

with open('datafile') as fin:
    user_info = dict(zip(file_keys, fin))
    # Now process user_info into your xml 

Of course this takes only one user from the file. 当然,这仅从文件中获取一个用户。 To get them all, you'll need a loop. 要获得所有这些,您需要一个循环。 You'll know you've got all your users once the user_info returned is an empty dictionary. 您将知道,一旦user_info返回的是空字典,您便拥有了所有用户。

with open('datafile') as fin:
    while True:
        user_info = dict(zip(file_keys, fin))
        if not user_info:  # empty dict.  we're done.
            break
        # Now process user_info into your xml

The reason why this works is because zip will truncate at the shorter of the two input iterables. 之所以起作用,是因为zip将在两个输入可迭代项中的较短者处截断。 In other words, it takes 1 element from file_keys and matches it with 1 line from the file. 换句话说,它从file_keys获取1个元素,并将其与文件中的1行匹配。 When file_keys runs out, it doesn't take any more lines, but the file object remembers it's position for the next read. file_keys用完时,它不再占用任何行,但是file对象会记住它在下一次读取时的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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