V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
yeyu1989
V2EX  ›  Python

pandas 读写 txt 报错

  •  
  •   yeyu1989 · 2018-06-04 11:30:13 +08:00 · 2499 次点击
    这是一个创建于 2124 天前的主题,其中的信息可能已经有所发展或是发生改变。

    pandas 读写 txt 时遇到的两个问题:

    1、txt 的分隔符为|,文档使用 gbk 编码,由于解码时存在特殊字符的 encode 后的字符串也有“|”,导致有一些行析出的列长度大于其他长度,因而读取失败;

    2、写入时,报错 UnicodeEncodeError: 'gbk' codec can't encode character '\u4dae' in position 151: illegal multibyte sequence

    请问大神们,应该怎么搞

    第 1 条附言  ·  2018-06-04 14:10:19 +08:00

    语句为:

    data = pd.read_table(filename, sep ='|', skiprows=startline, header=None, encoding='gb18030', skip_blank_lines=True, dtype=str, error_bad_lines=False, warn_bad_lines=True)

    data.to_csv(fw, index=False, encoding='gb18030', sep='|', header=False)

    第 2 条附言  ·  2018-06-04 14:14:28 +08:00

    追加中间对data的操作,其实就是将部分列置为空,然后排序

    for l in ln:
    
        data.loc[:,l]=''
    
    data.sort_values(by=[0,1,2], axis=0, inplace=True)
    
    11 条回复    2018-06-04 16:40:08 +08:00
    shnj
        1
    shnj  
       2018-06-04 13:20:31 +08:00
    第一个问题: 如果有问题的行不多且可以舍弃,可以使用 error_bad_lines 参数,ref: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html
    第二个问题没遇到过,但是网上有些解决方案,改变标准输出的默认编码应该可以解决。
    chesterzzy
        2
    chesterzzy  
       2018-06-04 13:22:48 +08:00
    先把 gbk 转为 utf-8 再说。
    RicardoScofileld
        3
    RicardoScofileld  
       2018-06-04 13:48:05 +08:00
    你指定在 read 的时候,指定 names,这样的话就不会报错了
    yeyu1989
        4
    yeyu1989  
    OP
       2018-06-04 14:06:50 +08:00
    @shnj 嗯,这个参数试过,读缺失可以用。但还没来得及验证,第二个错就开始报了。不知道有没有什么关联。
    yeyu1989
        5
    yeyu1989  
    OP
       2018-06-04 14:10:31 +08:00
    @chesterzzy 这个怎么转?我附了语句了
    yeyu1989
        6
    yeyu1989  
    OP
       2018-06-04 14:12:16 +08:00
    @RicardoScofileld names : array-like, default None 是指这个参数么。这个好像是列名?
    imzhong
        7
    imzhong  
       2018-06-04 14:23:53 +08:00
    .encode('utf_8') 试试,不行 用.encode('gb18030',errors='replace')把无法处理的字符换成‘?’
    RicardoScofileld
        8
    RicardoScofileld  
       2018-06-04 15:02:40 +08:00
    @yeyu1989 对 你指定列名就好了
    yeyu1989
        9
    yeyu1989  
    OP
       2018-06-04 15:38:57 +08:00
    @RicardoScofileld 我使用了参数 header=None,所有列名都是 0,1,2,3 这些好像。有影响吗
    flyingghost
        10
    flyingghost  
       2018-06-04 15:49:38 +08:00
    sep 参数支持正则表达式。如果你的分隔符和字符串能用某种正则来做区分,那可以把单纯的"|"字符替换成正则来做分割。
    第二个问题,这确实不是合法字符啊。最好是搞清楚来源是什么。很有可能是读时就读错了,最好解决掉。否则这行数据有可能是脏的。
    RicardoScofileld
        11
    RicardoScofileld  
       2018-06-04 16:40:08 +08:00
    @yeyu1989 这是默认生成的,你最好自己指定
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3494 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 11:02 · PVG 19:02 · LAX 04:02 · JFK 07:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.