晚上好,
我目前正在参加python的入门课程,并遇到了一个我无法解决的问题。我确信在我的代码中某处出现了一个简单的错误,但我无法找到任何解决我问题的问题。
奇怪的是,从cygwin执行它时编译并运行正常......
我在通过第三方测试(我无权访问)进行验证时遇到此错误:
Python脚本,ASCII文本可执行文件,带有CRLF行终止符
这是我的代码:
height = float(input("What is the plane's elevation in metres? \r\n"))
height = format(height * 3.28084, '.2f')
speed = float(input("What is the plane's speed in km/h? \r\n"))
speed = format(speed * 0.62137, '.2f')
temperature = float(input("Finally, what is the temperature (in celsius) outside? \r\n"))
temperature = format(temperature * (9/5) + 32, '.2f')
print("""\n########### OUTPUT ###########\n\nThe elevation is {feet} above the sea level, \n
you are going {miles} miles/h, \n
finally the temperature outside is {temp} degrees fahrenheit \n
########### OUTPUT ###########""".format(feet=height, miles=speed, temp=temperature))
这是一个基于它的cgi(两者都抛出相同的错误):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# To write pagecontent to sys.stdout as bytes instead of string
import sys
import codecs
#Enable debugging of cgi-.scripts
import cgitb
cgitb.enable()
# Send the HTTP header
#print("Content-Type: text/plain;charset=utf-8")
print("Content-Type: text/html;charset=utf-8")
print("")
height = format(1100 * 3.28084, '.2f')
speed = format(1000 * 0.62137, '.2f')
temperature = format(-50 * (9/5) + 32, '.2f')
toPrint = """\n########### OUTPUT ###########\n\nThe elevation is {feet} above the sea level, \n
you are going {miles} miles/h, \n
finally the temperature outside is {temp} degrees fahrenheit \n
########### OUTPUT ###########"""
toPrint.format(feet=height, miles=speed, temp=temperature)
# Here comes the content of the webpage
content = """<!doctype html>
<meta charset="utf-8">
<title>Min redovisnings-sida</title>
<pre>
<h1>Min Redovisnings-sida </h1>
</pre>
<body>
{printer}
</body>
"""
# Write page content
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
sys.stdout.write(content.format(printer=toPrint))
答案 0 :(得分:1)
您需要将CRLF转换为LF,为此您可以运行此命令:
a
如果您需要将其应用于特定文件夹内容,请使用文件夹中的以下命令:
dos2unix your_file
您需要先安装 dos2unix 包:
find . -type f -exec dos2unix {} \;