Categories
Python Answers

How to ignore the first line of data when processing CSV data with Python?

Sometimes, we want to ignore the first line of data when processing CSV data with Python.

In this article, we’ll look at how to ignore the first line of data when processing CSV data with Python.

How to ignore the first line of data when processing CSV data with Python?

To ignore the first line of data when processing CSV data with Python, we call next to skip the header row.

For instance, we write

import csv

with open('all16.csv', 'r', newline='') as file:
    has_header = csv.Sniffer().has_header(file.read(1024))
    file.seek(0)
    reader = csv.reader(file)
    if has_header:
        next(reader)
    column = 1
    data = (float(row[column]) for row in reader)
    least_value = min(data)

to write

has_header = csv.Sniffer().has_header(file.read(1024))

to see if the csv file has a header row with csv.Sniffer.

Then we have

file.seek(0)

to rewind the file to the beginning.

Next, we run next(reader) if has_header is True to skip the header row if it exists.

Conclusion

To ignore the first line of data when processing CSV data with Python, we call next to skip the header row.

Categories
Python Answers

How to print list items with Python?

Sometimes, we want to print list items with Python.

In this article, we’ll look at how to print list items with Python.

How to print list items with Python?

To print list items with Python, we can call print with the sep argument set to separator string to separate each item being printed.

For instance, we write

print(*my_list, sep='\n')

to print all the items in my_list separated by a new line character.

We use * to unpack the items in my_list list as arguments.

Conclusion

To print list items with Python, we can call print with the sep argument set to separator string to separate each item being printed.

Categories
Python Answers

How to enable CORS on Python Django REST Framework?

Sometimes, we want to enable CORS on Python Django REST Framework

In this article, we’ll look at how to enable CORS on Python Django REST Framework.

How to enable CORS on Python Django REST Framework?

To enable CORS on Python Django REST Framework, we use the django-cors-headers package.

To install it, we run

python -m pip install django-cors-headers

Then we use it by writing

INSTALLED_APPS = (
    #...
    'corsheaders',
    #...
)

in settings.py.

And we add the middleware by writing

MIDDLEWARE = [
    #...,
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    #...,
]

And we add the domains that we allow CORS from with

CORS_ALLOWED_ORIGINS = [
    'http://localhost:3030',
]

Conclusion

To enable CORS on Python Django REST Framework, we use the django-cors-headers package.

Categories
Python Answers

How to access elements of Python dictionary by index?

Sometimes, we want to access elements of Python dictionary by index

In this article, we’ll look at how to access elements of Python dictionary by index.

How to access elements of Python dictionary by index?

To access elements of Python dictionary by index, we can use the key to access the item.

For instance, we write

my_dict = {
    "Apple": {"American": "16", "Mexican": 10, "Chinese": 5},
    "Grapes": {"Arabian": "25", "Indian": "20"},
}

a = my_dict["Apple"]

We use my_dict["Apple"] to get the value of the 'Apple' key in my_dict.

Therefore, a is {"American": "16", "Mexican": 10, "Chinese": 5}.

Conclusion

To access elements of Python dictionary by index, we can use the key to access the item.

Categories
Python Answers

How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium with Python?

Sometimes, we want to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium with Python.

In this article, we’ll look at how to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium with Python.

How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium with Python?

To configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium with Python, we can set the headless option to True.

For instance, we write

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.headless = True
driver = webdriver.Chrome(CHROMEDRIVER_PATH, options=options)

to create a Options object.

Then we set options.headless to True to make the browser run headless.

Then we initiate the chrome driver with the options with

driver = webdriver.Chrome(CHROMEDRIVER_PATH, options=options)

Then Chrome will run headless.

Conclusion

To configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium with Python, we can set the headless option to True.