晚上好,
我目前正在参加 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))
Run Code Online (Sandbox Code Playgroud)
这是一个基于它的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))
Run Code Online (Sandbox Code Playgroud)
您需要将 CRLF 转换为 LF,为此您可以运行以下命令:
dos2unix your_file
Run Code Online (Sandbox Code Playgroud)
如果您需要将其应用于特定文件夹内容,请在文件夹中使用以下命令:
find . -type f -exec dos2unix {} \;
Run Code Online (Sandbox Code Playgroud)
您需要先安装dos2unix软件包:
sudo apt-get install dos2unix
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17939 次 |
| 最近记录: |