Categories
Python Answers

How to convert JSON data into a Python object?

Sometimes, we want to convert JSON data into a Python object.

In this article, we’ll look at how to convert JSON data into a Python object.

How to convert JSON data into a Python object?

To convert JSON data into a Python object, we can use the json.loads method.

For instance, we write

import json
from types import SimpleNamespace

data = '{"name": "John Smith", "hometown": {"name": "Chicago", "id": 123}}'

x = json.loads(data, object_hook=lambda d: SimpleNamespace(**d))

to call json.loads with data to parse the data JSON string into a dict.

We set the object_hook argument to a function that returns a SimpleNamespace object to convert the dict into a Python object.

Then we can access dict values from x like x.hometown.name.

Conclusion

To convert JSON data into a Python object, we can use the json.loads method.

Categories
Python Answers

How to run JavaScript in Selenium using Python?

Sometimes, we want to run JavaScript in Selenium using Python.

In this article, we’ll look at how to run JavaScript in Selenium using Python.

How to run JavaScript in Selenium using Python?

To run JavaScript in Selenium using Python, we can use the execute_script method.

For instance, we write

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://foo.com") 
driver.execute_script("document.getElementsByClassName('user')[0].click()")

to open the http://foo.com website with driver.get.

Then we call execute_script with the JavaScript code that we want to run in the string.

Conclusion

To run JavaScript in Selenium using Python, we can use the execute_script method.

Categories
Python Answers

How to convert Python Pandas DataFrame to list of lists?

Sometimes, we want to convert Python Pandas DataFrame to list of lists.

In this article, we’ll look at how to convert Python Pandas DataFrame to list of lists.

How to convert Python Pandas DataFrame to list of lists?

To convert Python Pandas DataFrame to list of lists, we can use the tolist method.

For instance, we write

df = pd.DataFrame([[1,2,3],[3,4,5]])
lol = df.values.tolist()

to call df.values.tolist to return a nested list with the values in the df data frame.

Conclusion

To convert Python Pandas DataFrame to list of lists, we can use the tolist method.

Categories
Python Answers

How to print list elements on separate lines in Python?

Sometimes, we want to print list elements on separate lines in Python.

In this article, we’ll look at how to print list elements on separate lines in Python.

How to print list elements on separate lines in Python?

To print list elements on separate lines in Python, we can call print with the sep argument.

For instance, we write

import sys

print(*sys.path, sep="\n")

to call print with the items in the sys.path list as arguments with *.

And then we set the sep argument to \n' to use a new line as a separator after each item printed to print each entry in its own line.

Conclusion

To print list elements on separate lines in Python, we can call print with the sep argument.

Categories
Python Answers

How to edit specific line in text file in Python?

Sometimes, we want to edit specific line in text file in Python.

In this article, we’ll look at how to edit specific line in text file in Python.

How to edit specific line in text file in Python?

To edit specific line in text file in Python, we can call readlines to read all the lines in the text file.

And then we call writelines to write the new content into the same file after updating the file.

For instance, we write

with open('stats.txt', 'r') as file:
    data = file.readlines()

data[1] = 'foo\n'

with open('stats.txt', 'w') as file:
    file.writelines(data)

to call open to open stats.txt.

Then we call readlines to read in the whole file as a list.

Then we change 2nd line with

data[1] = 'foo\n'

Next, we open the same file with write permission with open.

And then we call writelines with data to write the updated content into the file.

Conclusion

To edit specific line in text file in Python, we can call readlines to read all the lines in the text file.

And then we call writelines to write the new content into the same file after updating the file.