(For myself) Flask_5 (Add to txt file)

item

  1. Add to txt file
  2. Quiet talk _ Delete or overwrite any contents of the txt file

1. Add to txt file

html


<form action="/result" method="post">
        <label for="article">Post</label>
        <input type="text" name="article">
        <p></p>
        <label for="name">name</label>
        <input type="text" name="name">
        <button type="submit">Send</button>
    </form>

――This is the code that sends the input contents that you did last time --Saved in ʻarticleandname` respectively

python


article = request.form["article"]
name = request.form["name"]

file = codecs.open("articles.txt", "a", "utf-8")
file.write(article + "," + name + "\n")
file.close()

--You can put this in any route --ʻArticle = request.form ["article"] and name = request.form [" name"] are receiving the input and saved data as you did last time. --file = codecs.open ("articles.txt", "a", "utf-8") `` "a" is the mode to add --Declare to write with .write, separate with, with (article + "," + name + "\ n") , and write line by line by inserting \ n`

2. Quiet talk _ Delete or overwrite any contents of the txt file

--The read mode to put in .open () is

--Read-only mode with r --Write mode with w (this will completely overwrite and delete the previous data) -Mode added to the bottom with ʻa`

There is.

hoge.txt


foobarfoobar

When there is such a txt file,

python


open("hoge.txt","w").write(open("hoge.txt","r").read().replace("bar",""))

By

hoge.txt


foofoo

Can be done.

--This is because r refers to the contents of the txt file before writing with w.

3. At the end

――Honestly, I still don't understand much at the end

Recommended Posts

(For myself) Flask_5 (Add to txt file)
(For myself) Flask_8 (Add / Edit / Delete in database with python)
(For myself) Flask_4 (Drop-down menu, view txt data (using table))
(For myself) Flask_7 (Open database from Flask)
(For myself) Put Flask in VS Code
(For myself) Flask_3 (form, POST and GET)
python [for myself]
I want to add silence to the beginning of a wav file for 1 second
(For myself) AWS_Flask_3 (Install / Run Flask on AWS)
[ESXi (vCenter)] How to add NIC for CentOS 7.3
Unable to open file for writing About sudo
Add D (any other language) to SublimeLinter for highlighting
(For myself) Flask_2 (list and for, extends, and a little more)
Add convolution to MNIST
Easy to use Flask
heroku memo (for myself)
Introduction to Flask Part 1: First try running it locally & create an executable file for distribution
Freecad memorandum (for myself)
I want to exchange gifts even for myself! [Christmas hackathon]
(For myself) Flask_6 (Open db from python, Mysql basic (phpMyAdmin))
From environment construction to deployment for flask + Heroku with Docker
Convert Excel file to text in Python for diff purposes