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
Lcys
V2EX  ›  Python

Python 如何解析文件中的转义字符

  •  1
     
  •   Lcys · 2017-07-21 09:53:46 +08:00 · 3416 次点击
    这是一个创建于 2443 天前的主题,其中的信息可能已经有所发展或是发生改变。
    1.txt 内容\r\n\x80 之类的
    f = open("1.txt", "r")
    data = f.read()
    print data
    如何处理 data 能解析文件中的转义字符
    8 条回复    2017-07-21 17:22:48 +08:00
    CryMeatel
        1
    CryMeatel  
       2017-07-21 10:02:54 +08:00
    转义字符读进来本身默认是以转义字符呈现的哇~
    Kilerd
        2
    Kilerd  
       2017-07-21 10:14:58 +08:00 via iPhone
    你的意思是在文本中 \n 是两个字符。可是你读入的时候希望读成一个衣服?
    Kilerd
        3
    Kilerd  
       2017-07-21 10:15:24 +08:00 via iPhone
    衣服 → 字符
    Lcys
        4
    Lcys  
    OP
       2017-07-21 10:15:49 +08:00
    @Kilerd
    Lcys
        5
    Lcys  
    OP
       2017-07-21 10:24:01 +08:00
    好像只能替换- -
    weyou
        6
    weyou  
       2017-07-21 10:39:49 +08:00 via Android   ❤️ 1
    rb'abc\ncd\'efg'.decode('unicode_escape')
    araraloren
        7
    araraloren  
       2017-07-21 12:43:32 +08:00
    @Lcys 如果 \n 是存在文本中的两个字符,就不叫转义字符了。。
    你直接说如何处理 形式 如 '\n' '\x80' '\r' 的字符序列 为转义字符 就好了。。
    Lcys
        8
    Lcys  
    OP
       2017-07-21 17:22:48 +08:00
    @araraloren 对- -
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5314 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 08:07 · PVG 16:07 · LAX 01:07 · JFK 04:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.