Forms in Python

0

I needed a help with cgi forms in python. This code works normally when I do not enter 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 just do not write anything in the file when I put accents. Can someone help me save field data into a file?

Note: I use the XAMPP server, version 3.2.2 with apache version 2.4.29 and python version 3.6.2

    
asked by anonymous 21.01.2018 / 14:26

1 answer

0

Welcome to Stack Overflow in English!

When the variables name and password pass print , do they print correctly? If so, the problem is probably in open in its encoding.

Take a look at this link about encoding. Maybe it solves your problem:

with open("file.txt", "w", encoding='utf-8') as f:
    f.write("name: "+name+"\n"+"password: "+password)
    
11.02.2018 / 14:59