Categories
Python Answers

How to parse a JSON response from the Python requests library?

Sometimes, we want to parse a JSON response from the Python requests library.

In this article, we’ll look at how to parse a JSON response from the Python requests library.

How to parse a JSON response from the Python requests library?

To parse a JSON response from the Python requests library, we can use the response.json method.

For instance, we write:

import requests

response = requests.get('https://yesno.wtf/api')
json_data = response.json()
print(json_data)

We call requests.get with a URL.

Then we call response.json to return the JSON response as a dictionary.

And then we assign that to json_data.

Therefore, json_data is:

{'answer': 'yes', 'forced': False, 'image': 'https://yesno.wtf/assets/yes/8-2f93962e2ab24427df8589131da01a4d.gif'}

Conclusion

To parse a JSON response from the Python requests library, we can use the response.json method.

Categories
Python Answers

How to dump a NumPy array into a CSV file with Python?

Sometimes, we want to dump a NumPy array into a CSV file with Python

In this article, we’ll look at how to dump a NumPy array into a CSV file with Python

How to dump a NumPy array into a CSV file with Python?

To dump a NumPy array into a CSV file with Python, we can use the savetxt method.

For instance, we write:

import numpy
a = numpy.asarray([ [1,2,3], [4,5,6], [7,8,9] ])
numpy.savetxt("foo.csv", a, delimiter=",")

We call numpy.asarray with a nested list to create the a NumPy array.

Then we call savetxt with the path to the file we want to save to, the a array, and the delimiter for the cells.

As a result, in foo.txt, we get:

1.000000000000000000e+00,2.000000000000000000e+00,3.000000000000000000e+00
4.000000000000000000e+00,5.000000000000000000e+00,6.000000000000000000e+00
7.000000000000000000e+00,8.000000000000000000e+00,9.000000000000000000e+00

Conclusion

To dump a NumPy array into a CSV file with Python, we can use the savetxt method.

Categories
Python Answers

How to unpack tuples in for loops with Python?

Sometimes, we want to unpack tuples in for loops with Python.

In this article, we’ll look at how to unpack tuples in for loops with Python.

How to unpack tuples in for loops with Python?

To unpack tuples in for loops with Python, we separate the tuple entries with commas.

For instance, we write:

x = [(1, 2), (3, 4), (5, 6)]
for a, b in x:
    print(a, b)

We have the list x which is a list of tuples.

Then we loop through each entry in x and unpack the tuples entry with a, b.

In the loop body, we print out the tuple entries.

And so we get:

1 2
3 4
5 6

printed.

Conclusion

To unpack tuples in for loops with Python, we separate the tuple entries with commas.

Categories
Python Answers

How to loop through DataFrames with Python Pandas?

Sometimes, we want to loop through DataFrames with Python Pandas.

In this article, we’ll look at how to loop through DataFrames with Python Pandas.

How to loop through DataFrames with Python Pandas?

To loop through DataFrames with Python Pandas, we can use the df.iterrows method.

For instance, we write:

import pandas as pd

df = pd.DataFrame({'col1': [1, 2, 3]})
for index, row in df.iterrows():
    print(index, row)

We create a DataFrame with pd.DataFrame with a dictionary as its argument.

Then we loop through the DataFrame rows returned by the df.iterrows method.

In the loop body, we unpack the index and row and print them.

Then we get:

0 col1    1
Name: 0, dtype: int64
1 col1    2
Name: 1, dtype: int64
2 col1    3
Name: 2, dtype: int64

from the print output.

Conclusion

To loop through DataFrames with Python Pandas, we can use the df.iterrows method.

Categories
Python Answers

How to extract the extension from the filename in Python?

Sometimes, we want to extract the extension from the filename in Python.

In this article, we’ll look at how to extract the extension from the filename in Python.

How to extract the extension from the filename in Python?

To extract the extension from the filename in Python, we can use the os.path.splittext method.

For instance, we write:

import os

filename, file_extension = os.path.splitext('/path/to/somefile.ext')
print(file_extension)

We call os.path.splittext with the path string we want to extract the file extension from.

Then we take the file_extension from the returned tuple.

Therefore, file_extension is '.ext'.

Conclusion

To extract the extension from the filename in Python, we can use the os.path.splittext method.