programing

문자열에서 모든 utf-8이 아닌 기호 삭제

iphone6s 2023. 5. 27. 09:56
반응형

문자열에서 모든 utf-8이 아닌 기호 삭제

저는 많은 양의 파일과 파서를 가지고 있습니다.제가 해야 할 일은 모든 utf-8 기호를 제거하고 데이터를 mongodb에 넣는 것입니다.현재 저는 이런 코드를 가지고 있습니다.

with open(fname, "r") as fp:
    for line in fp:
        line = line.strip()
        line = line.decode('utf-8', 'ignore')
        line = line.encode('utf-8', 'ignore')

왠지 아직도 오류가 납니다.

bson.errors.InvalidStringData: strings in documents must be valid UTF-8: 
1/b62010montecassianomcir\xe2\x86\x90ta0\xe2\x86\x90008923304320733/290066010401040101506055soccorin

이해가 잘 안 돼요.간단한 방법이 있습니까?

UPD: Python과 Mongo가 Utf-8 유효 문자열의 정의에 동의하지 않는 것 같습니다.

마지막 두 줄 대신 코드 줄 아래에서 시도하십시오.도움이 되길 바랍니다.

line=line.decode('utf-8','ignore').encode("utf-8")

이 스레드의 주석에서 언급한 바와 같이 python 3의 경우 다음을 수행할 수 있습니다.

line = bytes(line, 'utf-8').decode('utf-8', 'ignore')

'무시' 매개 변수는 문자를 디코딩할 수 없는 경우 오류가 발생하는 것을 방지합니다.

줄이 이미 바이트 개체인 경우(예:b'my string') 그러면 당신은 단지 그것을 디코딩하기만 하면 됩니다.decode('utf-8', 'ignore').

outf-8 문자 처리 예제

import string

test=u"\n\n\n\n\n\n\n\n\n\n\n\n\n\nHi <<First Name>>\nthis is filler text \xa325 more filler.\nadditilnal filler.\n\nyet more\xa0still more\xa0filler.\n\n\xa0\n\n\n\n\nmore\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nfiller.\x03\n\t\t\t\t\t\t    almost there \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nthe end\n\n\n\n\n\n\n\n\n\n\n\n\n"

print ''.join(x for x in test if x in string.printable)
with open(fname, "r") as fp:
for line in fp:
    line = line.strip()
    line = line.decode('cp1252').encode('utf-8')

언급URL : https://stackoverflow.com/questions/26541968/delete-every-non-utf-8-symbols-from-string

반응형