我有一个在线医生推荐的文本文件。在这份文件中,每个病人都会提出问题,医生也会给出答案。此外,问题id和链接也是共享的。我只关注文本文件中的某些标题,我希望将这些字段导出到熊猫数据文件中。txt文件如下:
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从这个文件,我只想得到病人的话和医生的答案。输出如下:
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行)。我只想把病人和医生的部分保存在数据里。我怎么能这么做?谢谢你的帮助。
发布于 2022-01-30 01:58:01
您可以使用regex来匹配方言的模式,然后可以修剪它们,例如,我编写了一些杂乱的代码。
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代码的输出如下所示
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....https://stackoverflow.com/questions/70910348
复制相似问题