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()                     #记得关闭读入的文件
    

  • 相关阅读:
    魔法变量*args 和 **kwargs
    windows下怎么安装protobuf for python
    正向代理与反向代理
    Python 中 "is" 与 "==" 操作有什么区别?
    用 Anaconda 完美解决 Python2 和 python3 共存问题
    Python爬虫实例
    安装包制作工具 SetupFactory使用2 API清单
    软件测试流程(Test Flow)
    从一个实例详解敏捷测试的最佳实践
    网络常用基础知识大全
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4180745.html
Copyright © 2011-2022 走看看