我正在尝试运行pyhton cgi。在Apache Wamp服务器上。代码的HTML端工作正常,但是当它转到python脚本时。我收到以下错误:
脚本“ test.py”的标头格式错误:标头错误:
FISCAL_WEEK_NUMBER。,引荐来源:http://localhost/test.html
我尝试在IDE上分别测试我的python代码,效果很好。我在这里找不到问题。下面是我的代码:
#!C:/Program Files (x86)/Python36-32/python.exe
import cgi
import os
import cgitb;
import pandas as pd
import teradata
import numpy as np
os.environ["USERNAME"] = "hjoshi"
form = cgi.FieldStorage()
# Get filename here.
fileitem = form['filename']
# Test if the file was uploaded
if fileitem.file:
fn = os.path.basename(fileitem.filename)
query = "insert into p_piw_stg.sales_goals_test_harsh (?, ?, ?, ?)" #insert query for database
input = pd.read_excel('C:/Users/hjoshi/Downloads/' + fn)
test = pd.DataFrame(input, columns = ["FISCAL_WEEK_NUMBER", 'STORE_NUMBER', 'DEPARTMENT', 'SALES_GOAL_AMOUNT']) #creating a dataframe
#print(test)
num_of_chunks = len(test)
host,username,password = 'someIP','elt_usr', 'elt_usr'
udaExec = teradata.UdaExec(appName="test", version="1.0", logConsole=False)
with udaExec.connect (method="odbc",system=host, username=username, password=password, driver="Teradata") as con:
chunks = np.array_split(test, num_of_chunks)
for i,row in enumerate(chunks):
print(row)
data = [tuple(x) for x in chunks[i].to_records(index=False)]
con.executemany(query, data, batch=True)
message = 'The file "' + fn + '" was uploaded successfully'
else:
message = 'No file was uploaded'
print ("""
Content-Type: text/html\n\r\n
<html>
<body>
<p>%s</p>
</body>
</html>
""" % (message,))
答案 0 :(得分:0)
我有了代码
找到了解决上述问题的方法print("Content-Type: text/html\n\r\n")
编写导入文件后,在右上角。代码无法识别应该排在最前面的html标头。