首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将特定部分从txt文件写入dataframe

问将特定部分从txt文件写入dataframe
EN

Stack Overflow用户
提问于 2022-01-29 22:40:20
回答 1查看 68关注 0票数 0

我有一个在线医生推荐的文本文件。在这份文件中,每个病人都会提出问题,医生也会给出答案。此外,问题id和链接也是共享的。我只关注文本文件中的某些标题,我希望将这些字段导出到熊猫数据文件中。txt文件如下:

代码语言:javascript
复制
Description Q. What should I do to get glowing fair skin?
        
Dialogue 
Patient: Hi doctor, My face and body color are different. My face is getting dark and black day-by-day. What should I do to increase my fairness and get glowing skin? Shall I use some night cream? 
Doctor: Hi. I have read your problem carefully. What is your nature of work? Will you get exposed to sunlight? ... Take care. 
    
id=13585 https://www.icliniq.com/qa/migraine-headaches/how-can-i-cure-one-sided-headache-with-vomiting
    
Description Q. How can I cure one sided headache with vomiting?
    
Dialogue
Patient: Hi doctor, I have one side headache. How to cure it? Will you please give your suggestion? During maigrane headache, I have vomiting. 
Doctor: Hello. You seem to be suffering from migraine.   
    
id=13586 https://www.icliniq.com/qa/diarrhea/what-is-causing-diarrhea-after-eating-a-spicy-dish

从这个文件,我只想得到病人的话和医生的答案。输出如下:

代码语言:javascript
复制
    Patient                                             Doctor
0   I am 26 years old. I just found out that I hav...   Hi. For further information consult a ...
1   I am a 46 year old male. My weight is 75 kg an...   Hi. I understand your problem. Revert ... 
2   Since five days, I am having non-radiating che...   ECG and chest x-ray. For further infor...

我想删除文本中的其他部分(描述和id行)。我只想把病人和医生的部分保存在数据里。我怎么能这么做?谢谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2022-01-30 01:58:01

您可以使用regex来匹配方言的模式,然后可以修剪它们,例如,我编写了一些杂乱的代码。

代码语言:javascript
复制
import re
text = """Description Q. What should I do to get glowing fair skin?
        
Dialogue 
Patient: Hi doctor, My face and body color are different. My face is getting dark and black day-by-day. What should I do to increase my fairness and get glowing skin? Shall I use some night cream? 
Doctor: Hi. I have read your problem carefully. What is your nature of work? Will you get exposed to sunlight? ... Take care. 
    
id=13585 https://www.icliniq.com/qa/migraine-headaches/how-can-i-cure-one-sided-headache-with-vomiting
    
Description Q. How can I cure one sided headache with vomiting?
    
Dialogue
Patient: Hi doctor, I have one side headache. How to cure it? Will you please give your suggestion? During maigrane headache, I have vomiting. 
Doctor: Hello. You seem to be suffering from migraine.   
    
id=13586 https://www.icliniq.com/qa/diarrhea/what-is-causing-diarrhea-after-eating-a-spicy-dish"""

matchespatient = re.finditer("Patient: (.*?)\n", text, re.DOTALL)
matchesdoctor = re.finditer("Doctor: (.*?)\n", text, re.DOTALL)

zip_object = zip(matchespatient, matchesdoctor)
cnt = 0

print("\tPatient\t\t\t\t\t\t\tDoctor")
for i,j in zip_object:
    pat = i.group(0)[9:-1]
    doc = j.group(0)[8:-1]
    print(f"{cnt}\t{pat[:46]}...\t{doc[:46]}...")
    cnt+=1

代码的输出如下所示

代码语言:javascript
复制
        Patient                                                 Doctor
0       Hi doctor, My face and body color are differen...       Hi. I have read your problem carefully. What i...
1       Hi doctor, I have one side headache. How to cu...       Hello. You seem to be suffering from migraine....
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70910348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档