【问题标题】:Decoding an encoded text file - Python解码编码的文本文件 - Python
【发布时间】:2015-03-04 01:20:56
【问题描述】:

所以假设我编写了一个方法,将文本文件编码成一些看起来像

的乱码

úÎúÞ<81>i<82>ran<81><83>there<81><84>with<85>carol<86>we<81><87>did

我对如何将内容放回正常的文本文件有 0 意义 i ran there with carol we did

开头的字符只是幻数,但我只想检查幻数并将单词取回文件中忽略数字。

【问题讨论】:

  • 你能在匹配 的正则表达式上进行拆分吗?然后,您可以将您的内容作为一个可以迭代的列表来重新创建字符串。如果“幻数”总是在开头,那么它们将在索引 0 处。
  • 幻数?你如何检查它们?

标签: python decode


【解决方案1】:

re.split 使用正确的模式就可以了:

import re
s='úÎúÞ<81>i<82>ran<81><83>there<81><84>with<85>carol<86>we<81><87>did'
L = re.split(r'<[\d<>]+>',s)
print(L)
print(' '.join(L[1:]))

输出:

['úÎúÞ', 'i', 'ran', 'there', 'with', 'carol', 'we', 'did']
i ran there with carol we did

【讨论】:

    【解决方案2】:

    使用re提取&gt;&lt;之间的单词:

    s = "úÎúÞ<81>i<82>ran<81><83>there<81><84>with<85>carol<86>we<81><87>did"
    
    import re
    r = re.compile(">(.*?)<|>(.*)")
    print(r.findall(s))
    

    如果最后一个单词没有被包裹在 >

    print(" ".join(("".join(x) for x in r.findall(s))))
    

    【讨论】:

      猜你喜欢
      • 2015-05-23
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      • 2021-12-13
      • 2013-04-05
      • 2014-07-10
      • 2019-02-04
      • 1970-01-01
      相关资源
      最近更新 更多