Mac上python中的新行和制表符 [英] New line and tab characters in python on mac

查看:164
本文介绍了Mac上python中的新行和制表符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在在Mac OS 10.7.3上的python外壳上打印一个字符串.

I am printing a string to the python shell on a mac os 10.7.3.

该字符串包含换行符\ n \ r和制表符\ t.

The string contains new line characters, \n\r, and tabs, \t.

我不确定100%是否在每个平台上使用了什么换行符,但是我尝试了每种组合(\ n,\ n \ r,\ r),换行符都印在了外壳上:

I'm not 100% sure what new line characters are used on each platform, however i've tried every combination (\n, \n\r, \r) and the newline characters are printed on the shell:

 'hello\n\r\tworld'

我不知道自己在做错什么,我们将不胜感激.

I don't know what i'm doing wrong, any help is appreciated.

谢谢

推荐答案

在您看来,换行符和回车符实际上是两个字符-反斜杠加一个普通字符.

What look to you like newlines and carriage returns are actually two characters each -- a back slash plus a normal character.

使用your_string.decode('string_escape')修复此问题:

>>> s = 'hello\\n\\r\\tworld'   # or s = r'hello\n\r\tworld'
>>> print s
hello\n\r\tworld
>>> print repr(s)
'hello\\n\\r\\tworld'
>>> print s.decode('string_escape')
hello
        world

这篇关于Mac上python中的新行和制表符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆