zoukankan      html  css  js  c++  java
  • 自然语言处理---新词发现---微博数据预处理2

    好吧,我low了,用Java一行行读进行处理,结果还是虚拟机内存溢出:

    Error occurred during initialization of VM
    Incompatible minimum and maximum heap sizes specified

    换python,曾经找过python一行行读入数据的资料,没用对那方法,以为没有,low了。加上时间有些久没用python,进度有些缓慢,只是也还好,正在执行着,进行所有数据的预处理。

    1.python正则匹配,re.compile,以及finditer()函数。

    2.字符集问题codecsu'中文转义',r'转义'不够。

    3.python打开文件open()函数

    4.读入问题,一行行读入readlines()函数,存到text中,写入函数write()

    #coding:utf-8
    import codecs
    import re
    #----------------------
    n=0
    p=re.compile(u'[^u4e00-u9fa5]')       #正则匹配非中文字符
    #----------------------
    #一行一行读取该文件
    with codecs.open(u"D:/shifengworld/NLP/NLP_project/新词发现/data/untreated_data/2012_7.csv") as f:
        text = f.readlines()
    #----------------------
    file_object=open(u"D:/shifengworld/NLP/NLP_project/新词发现/data/data_preproces/abc2.txt",'w')
    #----------------------
    for line in text:
        line=line.decode('utf-8')           #由于字符编码问题,须要把打开的文件解码为utf-8格式?凌乱了,对字符编码还不够了解
        for m in p.finditer(line):          #python正则匹配全部非中文字符
            line=line.replace(m.group(),' ')#全部非中文字符替换为空格
        line=line.strip(' ')
        file_object.write(line+'
    ')        #读入 文件,而且每读入一行,增加一个换行符
    #     print line,
    #     if n>6:
    #         break
    #     n=n+1
    file_object.close()                     #记得关闭读入的文件
    

  • 相关阅读:
    Android新手引导库推荐
    windbg 常调用指令
    通过Hook NtOpenProcess 函数实现反调试
    PE文件
    消息机制
    软件调试
    异常(2) --- 编译器对于SEH异常的拓展
    异常(1)
    等待对象
    进程与线程
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4180745.html
Copyright © 2011-2022 走看看