简体   繁体   English

在python 3.x中提取文本文件的特定部分

[英]Extract specific portion of a text file in python 3.x

How do I make the if statement to read from a specific location of a text file and stop at a specific point and then print it out. 如何使if语句从文本文件的特定位置读取并在特定位置停止然后打印出来。 for example, printing out one patient's data, not all the list. 例如,打印出一个患者的数据,而不是全部列表。 beginner programmer here. 初学者程序员在这里。 thank you 谢谢

 ID = input("please enter a refernce id to search for the patient : ")
 info = open("data.txt", 'r')
 if ID in info: 
 # This should return only one patient's information not all the text file   
 else:
    print("not in file")
info.close()

We would need to know the specific details of how the file is formatted to give an exact answer, but here is one way that may be helpful. 我们需要知道如何格式化文件的具体细节以给出确切的答案,但这是一种可能有用的方法。

Firstly, your 'info' is right now just a TextIOWrapper object. 首先,您的“信息”现在只是一个TextIOWrapper对象。 You can tell by running print(type(info)) . 您可以通过运行print(type(info))来判断。 You need to make it info = open('data.txt', 'r').read() to give you a string of the text, or info = open('data.txt', 'r').readlines() to give you a list of the text by line, if the format is just plain text. 您需要将其设置为info = open('data.txt', 'r').read()才能为您提供文本字符串,或者将info = open('data.txt', 'r').readlines() (如果格式仅为纯文本),则可以按行显示文本列表。

Assuming the data looks something like this: 假设数据看起来像这样:

Patient: Charlie
Age = 99
Description: blah blah blah
Patient: Judith
Age: 100
Description: blah blah blahs

You can do the following: 您可以执行以下操作:

First, find and store the index of the ID you are looking for. 首先,找到并存储您要查找的ID的索引。 Secondly, find and store the index of some string that denotes a new ID. 其次,找到并存储一些表示新ID的字符串的索引。 In this case, that's the word 'Patient'. 在这种情况下,这就是“患者”一词。 Lastly, return the string between those two indices. 最后,返回这两个索引之间的字符串。

Example: 例:

ID = input("please enter a reference id to search for the patient: ")
info = open("data.txt", 'r').read()
if ID in info:
    #find() returns the beginning index of a string
    f = info.find(ID)
    goods = info[f:]
    l = goods.find('Patient')
    goods = goods[:l]
    print(goods)   
else:
    print("not in file")

Something along those lines should do the trick. 遵循这些原则可以解决问题。 There are probably better ways depending on the structure of the file. 根据文件的结构,可能有更好的方法。 Things can go wrong if the user input is not specific enough, or the word patient is scattered in the descriptions, but the idea remains the same. 如果用户的输入不够具体,或者描述中分散了“病人”一词,那么事情可能会出错,但是想法仍然相同。 You should do some error handling for the input, as well. 您还应该对输入进行一些错误处理。 I hope that helps! 希望对您有所帮助! Good luck with your project. 祝您项目顺利。

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

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