2015-02-10 74 views
0

我有一个非常大的数据集(stackoverflow的数据转储之一),这是完全原始和消毒形式。未验证数据的最佳方式是什么?

For example: </p> 

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

+0

这是太通用了。许多语言都有这样的特点。另外,如果您使用正确的XML解析器,那么这些转义将不会显示在您的字符串中 – 2015-02-10 22:32:00

+0

我正在要求一种方式来不转换消毒 - 我不在乎如何。 PS 30Gb – mcdoomington 2015-02-10 22:34:02

回答

0

这里是一个解决方案,我不得不用得到的一切工作正常 - 注意,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]) 
相关问题