RTF 파일의 줄을 일련의 유니 코드 문자열로 변환하려고 시도 중입니다. 줄에 정규식 일치를 수행하려고합니다. (나는 그들을 다른 파일로 출력 할 수 있도록 유니 코드로 만들어야합니다.)Python : RTF 파일을 유니 코드로 변환 하시겠습니까?
그러나 정규 표현식은 작동하지 않습니다. 유니 코드로 올바르게 변환되지 않았기 때문에 생각합니다. 이 모든 라인을 인쇄, 순간
usefulLines = []
textData = {}
# the regex pattern for an entry in the db (e.g. SUF 76,22): it's sufficient for us to match on three upper-case characters plus a space
entryPattern = '^([A-Z]{3})[\s].*$'
f = open('textbase_1a.rtf', 'Ur')
fileLines = f.readlines()
# get the matching line numbers, and store in usefulLines
for i, line in enumerate(fileLines):
#line = line.decode('utf-16be') # this causes an error: I don't really know what file encoding the RTF file is in...
line = line.decode('mac_roman')
print line
if re.match(entryPattern, line):
# now retrieve the following lines, all the way up until we get a blank line
print "match: " + str(i)
usefulLines.append(i)
하지만 경기에 아무것도 인쇄되지 않습니다 - 그것은 일치해야하지만 :
여기 내 코드입니다. 또한, 어떤 이유로 라인 시작시 '/ par'로 인쇄됩니다. 출력 파일에 인쇄 할 때 매우 이상하게 보입니다.
문제의 일부는 인코딩을 지정해야할지 모르겠다는 것입니다. 어떻게 알 수 있습니까?
내가 entryPattern = '^.*$'
을 사용하면 일치 항목을 얻습니다.
아무도 도와 줄 수 있습니까?
rtf 파일을 구문 분석하기 위해 정규식을 사용하지 마십시오. – Brian