我想在python中运行一个简单的映射器代码,unix没有识别我的shebang行,我在很多论坛中搜索过,建议添加sehbang行并授予该文件权限。我做了两个,但仍然无法正常工作。它只在我在文件之前添加python时才有效。
hduser@master:~/code$ ls
mapper.py reducer.py
hduser@master:~/code$ ls -l
total 8
-rwxrwxr-x 1 hduser hduser 225 Sep 16 07:57 mapper.py
-rw-rw-r-- 1 hduser hduser 663 Sep 16 07:56 reducer.py
hduser@master:~/code$ echo "foo foo quux labs foo bar quux" | python /home/hduser/code/mapper.py
foo 1
foo 1
quux 1
labs 1
foo 1
bar 1
quux 1
hduser@master:~/code$ which python
/usr/bin/python
hduser@master:~/code$ echo "foo foo quux labs foo bar quux" | /home/hduser/code/mapper.py
/home/hduser/code/mapper.py: line 5:
Created on 16/09/2014
@author: jee
: No such file or directory
/home/hduser/code/mapper.py: line 7: $'\r': command not found
/home/hduser/code/mapper.py: line 8: import: command not found
/home/hduser/code/mapper.py: line 9: $'\r': command not found
/home/hduser/code/mapper.py: line 11: syntax error near unexpected token `line'
'home/hduser/code/mapper.py: line 11: ` line = line.strip()
hduser@master:~/code$ vim mapper.py
'''
Created on 16/09/2014
@author: jee
'''
#!/usr/bin/python
import sys
for line in sys.stdin:
line = line.strip()
words = line.split()
for word in words:
print('%s\t%s' % (word, 1))
答案 0 :(得分:5)
#!必须在第一行:
#!/usr/bin/python
'''
Created on 16/09/2014
@author: jee
'''
import sys
for line in sys.stdin:
line = line.strip()
words = line.split()
for word in words:
print('%s\t%s' % (word, 1))
答案 1 :(得分:4)
shebang必须进入代码的第一行。
目前看来,你正在创建一个python语句(三引号),然后导入shebang。
所以,正确的版本将是
#!/usr/bin/python
'''
Created on 16/09/2014
@author: jee
'''
import sys
for line in sys.stdin:
line = line.strip()
words = line.split()
for word in words:
print('%s\t%s' % (word, 1))
<强> 修改 强>
你的shebang系列中有一个额外的角色,即Windows文件结束字符。删除它,你应该很高兴去。您可以查看this link以了解如何更改notepadd ++中的编码。