0
문자열이 이미 지정된 파일에 포함되어 있는지 확인하는 데 사용할 함수가 있습니다.중복 검사 기능에서 인코딩 오류가 발생했습니다.
def check_dupe(filename, word):
print(filename)
print(word)
with open(filename, 'rb', 0) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
if s.find(word.encode()) != -1:
print('dupe')
return True
나는 다음과 같은 오류가 :
<_io.TextIOWrapper name='links' mode='r' encoding='ANSI_X3.4-1968'>
Traceback (most recent call last):
File "checker.py", line 277, in <module>
main()
File "checker.py", line 261, in main
text_links = [line.strip() for line in f if not check_dupe('completed_links', f)]
File "checker.py", line 261, in <listcomp>
text_links = [line.strip() for line in f if not check_dupe('completed_links', f)]
File "checker.py", line 240, in check_dupe
if s.find(word.encode()) != -1:
AttributeError: '_io.TextIOWrapper' object has no attribute 'encode'
어떻게 이런 일이 발생을 방지 할 수 있습니다
기능은 다음과 같습니다?
당신의'word'을 수행합니다 나는 당신이 참으로 문자열 인 대신 매개 변수로
line
를 전달하는 의미 추측_io.TextIOWrapper
반환 ("파일 이름") 개방으로 열린 파일 추측 에서 오는 ? ('check_dupe()'에 매개 변수로 전달 된 것). 나는 그것이 문자열이라고 생각하지만 stackstrace는'_io.TextIOWrapper'임을 보여줍니다. – Guillaume
추가 질문 : 파일이 텍스트 파일입니까, 바이너리 파일입니까? 그리고'mmap '을 사용할 특별한 이유가 있습니까? – Guillaume
@Guillaume 100 % 문자열이며'type()'으로 검사했습니다. 예, 단어가 인수로 전달됩니다. 내가 다른 방법보다 빠르다고 느꼈기 때문에 mmap을 사용하는 이유가 있습니다. 파일은 일반 텍스트 파일이지만 2 진 파일로 읽습니다. 파일을 한 단어를 확인할 수있는 빠른 방법이 있다면 나는 제안을 할 수 있습니다. – rhillhouse