gpt4 book ai didi

python - 提取 .txt 文件中两个关键字之间的所有单词

转载 作者:行者123 更新时间:2023-12-05 00:44:47 29 4
gpt4 key购买 nike

我想提取 .txt 文件中特定关键字内的所有单词。对于关键字,有一个起始关键字PROC SQL; (我需要这个不区分大小写)并且结束关键字可以是 RUN; , quit;QUIT; .这是我的 sample .txt file .

到目前为止,这是我的代码:

with open('lan sample text file1.txt') as file:
text = file.read()
regex = re.compile(r'(PROC SQL;|proc sql;(.*?)RUN;|quit;|QUIT;)')
k = regex.findall(text)
print(k)

输出:
[('quit;', ''), ('quit;', ''), ('PROC SQL;', '')]

然而,我的 预期输出 是获取关键字之间和包含关键字的单词:
proc sql; ("TRUuuuth");
hhhjhfjs as fdsjfsj:
select * from djfkjd to jfkjs
(
SELECT abc AS abc1, abc_2_ AS efg, abc_fg, fkdkfj_vv, jjsflkl_ff, fjkdsf_jfkj
FROM &xxx..xxx_xxx_xxE
where ((xxx(xx_ix as format 'xxxx-xx') gff &jfjfsj_jfjfj.) and
(xxx(xx_ix as format 'xxxx-xx') lec &jgjsd_vnv.))
);

1)

jjjjjj;

select xx("xE'", PUT(xx.xxxx.),"'") jdfjhf:jhfjj from xxxx_x_xx_L ;
quit;

PROC SQL; ("CUuuiiiiuth");
hhhjhfjs as fdsjfsj:
select * from djfkjd to jfkjs
(SELECT abc AS abc1, abc_2_ AS efg, abc_fg, fkdkfj_vv, jjsflkl_ff, fjkdsf_jfkj
FROM &xxx..xxx_xxx_xxE
where ((xxx(xx_ix as format 'xxxx-xx') gff &jfjfsj_jfjfj.) and
(xxx(xx_ix as format 'xxxx-xx') lec &jgjsd_vnv.))(( ))
);

2)(

RUN;

任何建议或不同的方法来解决这个问题将不胜感激!

实现用户@Finefoot 的代码后的输出:
enter image description here

但是,有没有办法将线条分开,看起来像这样?:

enter image description here

最佳答案

这对我有用:

import re

with open('lan sample text file1.txt') as file:
condition = False
text_to_return = ""
for line in file:
if condition == True:
if line[0:5].lower() == "quit;" or line[0:4].upper() == "RUN;":
condition = False
text_to_return += line
if line[0:9].upper() == "PROC SQL;":
condition = True
text_to_return += line

output_file = open("output.txt", "w")
output_file.write(text_to_return)
output_file.close()

这是您可以接受的解决方案吗?

关于python - 提取 .txt 文件中两个关键字之间的所有单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59290631/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com