【问题标题】:What is the best way to unconvert sanitized data?取消转换已清理数据的最佳方法是什么?
【发布时间】:2015-02-13 03:10:44
【问题描述】:

我有一组非常大的数据(stackoverflow 的数据转储之一),它们完全是原始和经过清理的形式。

For example:  </p>

是否有一种已经确立的方法可以将上述和类似的内容转换回其原始形式以提高可读性和可用性?偶然的python脚本或函数调用?

【问题讨论】:

  • 这太笼统了。许多语言都有这样的特点。此外,如果您使用适当的 XML 解析器,这些转义将不会首先出现在您的字符串中
  • 我在寻求一种方法来取消消毒 - 我不在乎如何。 PS它的30Gb

标签: xml database sanitization


【解决方案1】:

这是我必须使用的解决方案,以使一切正常运行 - 请注意,HTML 解析器并没有对我的数据集执行我想要的所有操作

!/usr/bin/python3

    import html.parser
    import string
    import sys
    
    # Amount of lines to put into a buffer before writing
    BUFFER_SIZE_LINES = 1024
    html_parser = html.parser.HTMLParser()
    
    # Few HTML reserved chars that are not being cleaned up by HTMLParser
    dict = {}
    dict[ '"' ] = '"'
    dict[ ''' ] = "'"
    dict[ '&' ] = '&'
    dict[ '&lt;' ] = '<'
    dict[ '&gt;' ] = '>'
    
    # Process the file
    def ProcessLargeTextFile(fileIn, fileOut):
        r = open(fileIn, "r")
        w = open(fileOut, "w")
        buff = ""
        buffLines = 0
        for lineIn in r:
    
            lineOut = html_parser.unescape(lineIn)
            for key, value in dict.items():
                lineOut = lineOut.replace(key,value)
    
            buffLines += 1
            
            if buffLines >= BUFFER_SIZE_LINES:
                w.write(buff)
                buffLines = 1
                buff = ""
    
            buff += lineOut + "\n"
    
        w.write(buff)
        r.close()
        w.close()
    
    
    # Now run
    ProcessLargeTextFile(sys.argv[1],sys.argv[2])
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 2010-09-07
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多