首页 > 未分类 > python的怪问题

python的怪问题

2007年3月20日 发表评论 阅读评论

这两天在学python
发现有个比较奇怪的问题
我从命令行参数中传进去的回车符”\n”跟程序里本身的字符串中含的”\n”表现不一样
比如简单程序t.py
#!/usr/bin/python

import sys

str='a\nb\nc'
print str
str1=sys.argv[1]
print str1
print sys.argv[1]

# ./t.py “a\nb\nc”执行的时候
系统输出:

a
b
c
a\nb\nc
a\nb\nc


不知道为什么

原创文章,转载请注明: 转载自嘻嘻哈哈的部落格(blog)

本文链接地址: python的怪问题

分类: 未分类 标签: , ,
  1. 2009年3月24日21:48 | #1

    这个是由于shell的转义造成的:

    import sys

    str = ‘a\nb\nc’

    print str

    str1= sys.argv[1]
    tmp = []
    tmp.append(str1)
    print tmp

    试着运行这段代码,结果:

    [cocobear@cocobear ~]$ ./foo.py ‘a\nb\nc’
    a
    b
    c
    ['a\\nb\\nc']

  1. 本文目前尚无任何 trackbacks 和 pingbacks.

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word