3
I needed help with the cgi forms in python.
This code works normally when I don’t insert accents:
# !/Programas/Python36-32/python
# -*- coding: utf-8 -*-
import cgi
print("Content-Type: text/html; UTF-8\n")
form = cgi.FieldStorage()
web_page="""
<!DOCTYPE HTML>
<html>
<head>
<title>O Meu primeiro programa</title>
</head>
<body>
<form method="post">
Name: <input type="text" name="name"><br>
Password: <input type="text" name="password"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
"""
print(web_page)
name=form["name"].value
password=form["password"].value
print("name: "+name)
print("<br>")
print("password: "+password)
with open("file.txt", "w") as f:
f.write("name: "+name+"\n"+"password: "+password)
But it just doesn’t write anything in the file when I put accents in it. Can someone help me save the data of the fields to a file?
Note: I use the XAMPP server, version 3.2.2 with apache version 2.4.29 and python version 3.6.2
Have you tried defining the charset in HTML too? Try inserting
<meta charset="utf-8">
in the<head>
– fernandosavio