Categories
Python Answers

How to upload file with Python requests?

Sometimes, we want to upload file with Python requests.

In this article, we’ll look at how to upload file with Python requests.

How to upload file with Python requests?

To upload file with Python requests, we call requests.post with the files argument.

For instance, we write

files = {"upload_file": open("file.txt", "rb")}
values = {"DB": "photcat", "OUT": "csv", "SHORT": "short"}

r = requests.post(url, files=files, data=values)

to call requests.posts to make a POST request to the url.

We set the files argument to the files dict that has a file as a value in the entry to upload that file as form data.

And we set data to values to add those key-value pairs as form data.

Conclusion

To upload file with Python requests, we call requests.post with the files argument.

Categories
Python Answers

How to check if a string contains a number with Python?

Sometimes, we want to check if a string contains a number with Python.

In this article, we’ll look at how to check if a string contains a number with Python.

How to check if a string contains a number with Python?

To check if a string contains a number with Python, we can use the re.search method.

For instance, we write

import re
def has_numbers(input_string):
    return bool(re.search(r'\d', input_string))

x = has_numbers("I own 1 dog")

to create the has_numbers function.

In it, we call re.search with a regex string that matches digits in the input_string to see if input_string has any digits inside.

The we call has_numbers with a string.

And since it has a number inside, x is True.

Conclusion

To check if a string contains a number with Python, we can use the re.search method.

Categories
Python Answers

How to load and parse a JSON file with multiple JSON objects with Python?

Sometimes, we want to load and parse a JSON file with multiple JSON objects with Python.

In this article, we’ll look at how to load and parse a JSON file with multiple JSON objects with Python.

How to load and parse a JSON file with multiple JSON objects with Python?

To load and parse a JSON file with multiple JSON objects with Python, we can use open the file and parse the JSON object in each line.

For instance, we write

import json

data = []
with open('file') as f:
    for line in f:
        data.append(json.loads(line))

to open the file file with open.

And then we loop through each line in f with a for loop.

In the loop, we call data.append with the JSON loaded with json.loads(line) to append the dict with the parsed JSON into the data list.

Conclusion

To load and parse a JSON file with multiple JSON objects with Python, we can use open the file and parse the JSON object in each line.

Categories
Python Answers

How to serialize SQLAlchemy result to JSON with Python?

Sometimes, we want to serialize SQLAlchemy result to JSON with Python.

In this article, we’ll look at how to serialize SQLAlchemy result to JSON with Python.

How to serialize SQLAlchemy result to JSON with Python?

To serialize SQLAlchemy result to JSON with Python, we can add a method to return the model class content as a dict.

For instance, we weite

class User:
   def as_dict(self):
       return {c.name: getattr(self, c.name) for c in self.__table__.columns}

to create the User class that has the as_dict method that returns a dict that has all the properties in the dict.

The instance property names are the keys and the instance property values are values.

Conclusion

To serialize SQLAlchemy result to JSON with Python, we can add a method to return the model class content as a dict.

Categories
Python Answers

How to print string to text file with Python?

Sometimes, we want to print string to text file with Python.

In this article, we’ll look at how to print string to text file with Python.

How to print string to text file with Python?

To print string to text file with Python, we can use the write method.

For instance, we write

with open("output.txt", "w") as text_file:
    text_file.write("Purchase Amount: %s" % total)

to call open to open the output.txt with write permission.

Then we call text_file.write to append a line of text into the file.

The file is closed after writing the line since we used with with open.

Conclusion

To print string to text file with Python, we can use the write method.