How to manipulate html forms in python

Asked

Viewed 979 times

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

  • 1

    Have you tried defining the charset in HTML too? Try inserting <meta charset="utf-8"> in the <head>

1 answer

0

Welcome to Stack Overflow!

When the variables name and password go through the print, they are printed correctly? If yes, the problem is probably in the 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)
  • Hello, it didn’t work, it still doesn’t write anything in the file, not even "name: " and "password: " and I can’t print with the print values, only if encoding for an Encode and use the function repr and then I can’t decode into a readable format.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.