Categories
Python Answers

How to append new row to old csv file with Python?

Sometimes, we want to append new row to old csv file with Python.

In this article, we’ll look at how to append new row to old csv file with Python.

How to append new row to old csv file with Python?

To append new row to old csv file with Python, we can open the file with append permission and then use the write method to append the new row.

For instance, we write

with open('document.csv','a') as fd:
    fd.write(csv_row)

to open the document.csv file with 'a' permission to let us append to the file.

Then we call write with the csv_row string to write the row to the csv file.

Conclusion

To append new row to old csv file with Python, we can open the file with append permission and then use the write method to append the new row.

Categories
Python Answers

How to get IP address of visitors using Flask for Python?

Sometimes, we want to get IP address of visitors using Flask for Python.

In this article, we’ll look at how to get IP address of visitors using Flask for Python.

How to get IP address of visitors using Flask for Python?

To get IP address of visitors using Flask for Python, we can use the request.remote_addr property.

For instance, we write

from flask import request
from flask import jsonify

@app.route("/get_my_ip", methods=["GET"])
def get_my_ip():
    return jsonify({'ip': request.remote_addr}), 200

to get the client’s IP address with the request.remote_addr property.

Conclusion

To get IP address of visitors using Flask for Python, we can use the request.remote_addr property.

Categories
Python Answers

How to access the query string in Flask routes with Python?

Sometimes, we want to access the query string in Flask routes with Python.

In this article, we’ll look at how to access the query string in Flask routes with Python.

How to access the query string in Flask routes with Python?

To access the query string in Flask routes with Python, we can use the request.query_string property.

For instance, we write

from flask import request

@app.route('/adhoc_test/')
def adhoc_test():

    return request.query_string

to get the query string with request.query_string within the adhoc_test view.

We use request.args.get('param') to get the value of the param query string parameter.

Conclusion

To access the query string in Flask routes with Python, we can use the request.query_string property.

Categories
Python Answers

How to open a file for both reading and writing with Python?

Sometimes, we want to open a file for both reading and writing with Python.

In this article, we’ll look at how to open a file for both reading and writing with Python.

How to open a file for both reading and writing with Python?

To open a file for both reading and writing with Python, we can call open with the 'r+' permission.

For instance, we write

with open(filename, "r+") as f:
    data = f.read()
    f.seek(0)
    f.write(output)
    f.truncate()

to call open with the filename and the 'r+' permission to open the file at filename with read and write permission.

Then we call read to read the file in the with block.

And we call write to write the output to the file.

Conclusion

To open a file for both reading and writing with Python, we can call open with the 'r+' permission.

Categories
Python Answers

How to encode a string according to a password with Python?

Sometimes, we want to encode a string according to a password with Python.

In this article, we’ll look at how to encode a string according to a password with Python.

How to encode a string according to a password with Python?

To encode a string according to a password with Python, we can use the cryptography library.

To install it, we run

pip install cryptography

Then we use it by writing

from cryptography.fernet import Fernet

key = Fernet.generate_key() 

from cryptography.fernet import Fernet

def encrypt(message: bytes, key: bytes) -> bytes:
    return Fernet(key).encrypt(message)

def decrypt(token: bytes, key: bytes) -> bytes:
    return Fernet(key).decrypt(token)

message = 'John Doe'
token = encrypt(message.encode(), key)

decoded = decrypt(token, key).decode()

to create the encryption key with generate_key.

And then we call encrypt in the encrypt function to encrypt the message byte string with the key.

In the decrypt function, we call decrypt with the token byte string to decrypt the string with the key.

And then we call decode to decode the byte string into the original string.

Conclusion

To encode a string according to a password with Python, we can use the cryptography library.