Categories
Python Answers

How to convert JSON to CSV with Python?

Sometimes, we want to convert JSON to CSV with Python.

In this article, we’ll look at how to convert JSON to CSV with Python.

How to convert JSON to CSV with Python?

To convert JSON to CSV with Python, we can use the Panda’s read_json method to convert a JSON string into a data frame.

Then we use to_csv to convert the data frame to a csv.

For instance, we write

import pandas as pd

with open('jsonfile.json', encoding='utf-8') as input_file:
    df = pd.read_json(input_file)

df.to_csv('csvfile.csv', encoding='utf-8', index=False)

to call pd.read_json with input_file to read the JSON file into a data frame.

Then we call to_csv with the file path to write to, the encoding, and we skip the index with index set to False to write the data frame content into a csv file.

Conclusion

To convert JSON to CSV with Python, we can use the Panda’s read_json method to convert a JSON string into a data frame.

Then we use to_csv to convert the data frame to a csv.

Categories
Python Answers

How to call a script from another script with Python?

Sometimes, we want to call a script from another script with Python.

In this article, we’ll look at how to call a script from another script with Python.

How to call a script from another script with Python?

To call a script from another script with Python, we can import the script with import.

For instance, we write

foo.py

def some_func():
    print('hello')

if __name__ == '__main__':
    some_func()

Then we run some_func in bar.py by writing

import foo

def service_func():
    print('func')

if __name__ == '__main__':
    service_func()
    foo.some_func()

We import foo.py with import foo.

And then we call foo.some_func to call the some_func function in foo.py.

Conclusion

To call a script from another script with Python, we can import the script with import.

Categories
Python Answers

How to remove specific characters from a string in Python?

Sometimes, we want to remove specific characters from a string in Python.

In this article, we’ll look at how to remove specific characters from a string in Python.

How to remove specific characters from a string in Python?

To remove specific characters from a string in Python, we call the string translate method.

For instance, we write

line = line.translate(None, '!@#$')

to call translate to remove the characters listed in '!@#$' and assign the returned string back to line.

Conclusion

To remove specific characters from a string in Python, we call the string translate method.

Categories
Python Answers

How to download image using requests with Python?

Sometimes, we want to download image using requests with Python.

In this article, we’ll look at how to download image using requests with Python.

How to download image using requests with Python?

To download image using requests with Python, we can use the requests.get method.

For instance, we write

import shutil

import requests

url = 'http://example.com/img.png'
response = requests.get(url, stream=True)
with open('img.png', 'wb') as out_file:
    shutil.copyfileobj(response.raw, out_file)

to call requests.get to make a GET request to the image url.

And then we write the file into the out_file with

shutil.copyfileobj(response.raw, out_file)

after opening img.ong with open.

Conclusion

To download image using requests with Python, we can use the requests.get method.

Categories
Python Answers

How to create decorators with parameters with Python?

Sometimes, we want to create decorators with parameters with Python.

In this article, we’ll look at how to create decorators with parameters with Python.

How to create decorators with parameters with Python?

To create decorators with parameters with Python, we create a function that returns a decorator function.

For instance, we write

def parametrized(dec):
    def layer(*args, **kwargs):
        def repl(f):
            return dec(f, *args, **kwargs)
        return repl
    return layer

to create the parameterized decorator that takes the dec function as its argument.

Then we return the layer function which calls dec.

Next, we can use it by writing

@parametrized
def multiply(f, n):
    def aux(*xs, **kws):
        return n * f(*xs, **kws)
    return aux

@multiply(2)
def function(a):
    return 10 + a

to create a decorator from the multiply function by using the parameterized decorator.

And then we can use the multiply decorator with function.

Conclusion

To create decorators with parameters with Python, we create a function that returns a decorator function.