37,743
社区成员




# -*- coding: UTF-8 -*-
import re
fo = open("1.txt", "r");
co = open("2.txt", "r");
colines = co.readlines();
for line in fo.readlines():
line = line.strip();
matchObj = re.search( line, "%s" % colines, re.M | re.I);
if matchObj:
print (line);
fo.close();
co.close();
# -*- coding: UTF-8 -*-
import re
fo = open("2.txt", "rb");
co = open("n1.txt", "rb");
colines = co.readlines();
for line in fo.readlines():
line = line.strip();
matchObj = re.search( line, "%s" % colines, re.M | re.I);
if matchObj:
print (line);
fo.close();
co.close();
这是修改后代码Traceback (most recent call last):
File "qq.py", line 12, in <module>
matchObj = re.search( line, "%s" % colines, re.M | re.I);
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\re.py", line 183, in search
return _compile(pattern, flags).search(string)
TypeError: cannot use a bytes pattern on a string-like object
大佬。就在我的代码基础上把r换成rb后报错,这个该怎么办
rsltList = []
print(key.decode('gbk'))
输出时,同时将找到的key保存入List
rsltList.append(key.decode("GBK"))
最后得到一个查找到的rsltList
用with open("rslt.txt","wb") as f:
打开输出文件
f.writelines 可以直接将rsltList保存到文件,注意:如果需要一行一个key,rsltList的每行的\r\n要手动添加给每个item
rsltList.appent(f'{key.decode("GBK")}\r\n')[/quote]
好的非常非常感谢大佬,解决了困扰好几天的问题,
rsltList = []
print(key.decode('gbk'))
输出时,同时将找到的key保存入List
rsltList.append(key.decode("GBK"))
最后得到一个查找到的rsltList
用with open("rslt.txt","wb") as f:
打开输出文件
f.writelines 可以直接将rsltList保存到文件,注意:如果需要一行一个key,rsltList的每行的\r\n要手动添加给每个item
rsltList.appent(f'{key.decode("GBK")}\r\n')
with open("source.txt", "rb")as f:
source = f.readlines()
sourceSt = b"".join(source)
with open("key.txt", "rb") as f:
for key in f:
key = key.strip()
if key in sourceSt:
print(key.decode('gbk'))
由于你的key是一行一个key这种特殊存在,可能这样写会更简洁,更快一点,大概可能50%的速度提升吧。
另外不建议查到就print,如果查到的key过多(比如key.txt中重复的很多),速度会很慢。
[/quote]大佬快给我感动哭了,快太多了简直 python可以在这种本身的代码上增加功能吗,比如说我要增加个找到了直接导出到一个TXT文本,或者直接在B文档中删除找到的词,
with open("source.txt", "rb")as f:
source = f.readlines()
sourceSt = b"".join(source)
with open("key.txt", "rb") as f:
for key in f:
key = key.strip()
if key in sourceSt:
print(key.decode('gbk'))
由于你的key是一行一个key这种特殊存在,可能这样写会更简洁,更快一点,大概可能50%的速度提升吧。
另外不建议查到就print,如果查到的key过多(比如key.txt中重复的很多),速度会很慢。
print(f"error key:{key.decode('gbk')}")
import re
with open("source.txt", "rb")as f:
source = f.readlines()
sourceSt = b"".join(source)
with open("key.txt", "rb") as f:
for key in f:
key = key.strip()
try:
mObj = re.search(key, sourceSt,re.M|re.I)
except Exception:
print(f"error key:{key.decode("gbk")}")
if mObj:
print(key.decode("gbk"))
import re
with open("source.txt", "rb")as f:
source = f.readlines()
sourceSt = b"".join(source)
with open("key.txt", "rb") as f:
for key in f:
key = key.strip()
try:
mObj = re.search(key, sourceSt,re.M|re.I)
except Exception:
print(f"error key:{key}")
if mObj:
print(key.decode("gbk"))
可能会对速度有影响,不过可以查key错误。