我從gentting HTML文件中獲取值並獲取 獲取TracebackError:我不知道該怎麼改?TypeError:參數1必須是字符串或只讀字符緩衝區,而不是無
Traceback (most recent call last):
File "test.py", line 33, in <module>
get_data()
File "test.py", line 25, in get_data
f.write(form.getvalue('firstname'))
TypeError: argument 1 must be string or read-only character buffer, not None
我的Python代碼:
# !/usr/bin/python
import cgi
def get_data():
'''
This function writes the HTML data into the file
'''
print "Content- type : text/html\n"
form = cgi.FieldStorage()
#Fname = form.getvalue('firstname')
#Lname = form.getvalue('lastname')
#Age = form.getvalue('age')
#Gender = form.getvalue('gender')
f = open("abc.txt","w")
f.write(form.getvalue('firstname'))
f.write(form.getvalue('lastname'))
f.write(form.getvalue('age'))
f.write(form.getvalue('gender'))
f.close()
#print "Hello ", Fname, Lname, Age, Gender
get_data()
我是一個新手。並無法獲得錯誤。請幫我與
HTML文件:
<html>
<head>
<title>INFORMATION</title>
</head>
<body>
<form action = "/cgi-bin/test.py" method = "post">
FirstName:
<input type = "text" name = "firstname" /><br>
LastName:
<input type = "text" name = "lastname" /><br>
Age:
<input type = "text" name = "age" /><br>
Gender:
<input type="radio" name="gender" value="male" /> Male
<input type="radio" name="gender" value="female" /> Female
<input type = "submit" name = "submit "value = "SUBMIT">
<input type = "reset" name = "reset" value = "RESET">
</form>
</body>
</html>
這不是你的問題,但值得注意的是最好[使用'with'語句](http://www.youtube.com/watch?v=lRaKmobSXF4)來處理文件。 –
似乎'firstname'字段爲空或缺失。你能否顯示產生要提交的表單的HTML? –
@Lattyware他使用Python 2.6(來自上一個問題),並且不能使用'with'。 –