Python:将RTF文件转换为unicode?

2024-10-01 09:26:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将RTF文件中的行转换为一系列unicode字符串,然后对这些行执行regex匹配。(我需要它们是unicode,这样我就可以将它们输出到另一个文件中。)

但是,我的regex匹配不起作用-我想是因为它们没有被正确地转换成unicode。在

我的代码是:

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)

目前,它打印所有的行,但不打印任何带有match的内容-尽管它应该匹配。此外,由于某些原因,这些行在开始时被打印为'/par'。当我尝试将它们打印到输出文件时,它们看起来很奇怪。在

部分问题是我不知道要指定什么编码。我怎么才能知道?在

如果我使用entryPattern = '^.*$',那么我确实得到了匹配项。在

有人能帮忙吗?在


Tags: 文件theinanforgetmatchline
1条回答
网友
1楼 · 发布于 2024-10-01 09:26:41

你甚至没有解码RTF文件。rtf是简单的文本文件。例如,包含“äü”的文件包含以下内容:

{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fswiss\fcharset0 Arial;}}

{*\generator Msftedit 5.41.15.1507;}\viewkind4\uc1\pard\f0\fs20\'e4\'f6\'fc\par

}

在文本编辑器中打开时。因此,字符“äü”被编码为windows-1252,正如文件开头所声明的那样(äü=0xE4 0xF6 0xFC)。在

要阅读RTF,首先需要将RTF转换为文本(已经是asked here)。在

相关问题 更多 >