Categories
Python Answers

How to make a string lowercase in Python?

Sometimes, we want to make a string lowercase in Python.

In this article, we’ll look at how to make a string lowercase in Python.

How to make a string lowercase in Python?

To make a string lowercase in Python, we can use the string lower method.

For instance, we write

s = "FooBar".lower()

to return a string with the letters in "FooBar" converted to lower case with lower.

Conclusion

To make a string lowercase in Python, we can use the string lower method.

Categories
Python Answers

How to read a huge .csv file with Python?

Sometimes, we want to read a huge .csv file with Python.

In this article, we’ll look at how to read a huge .csv file with Python.

How to read a huge .csv file with Python?

To read a huge .csv file with Python, we can use Pandas’ read_csv method.

For instance, we write

import pandas as pd

chunksize = 10 ** 8

for chunk in pd.read_csv(filename, chunksize=chunksize):
    process(chunk)

to call read_csv with the filename path to the csv file and the chunksize in bytes to read the csv file in chunks.

It returns an iterator that we can use to get the file chunks.

Then we get the csv file chunk from chunk.

Conclusion

To read a huge .csv file with Python, we can use Pandas’ read_csv method.

Categories
Python Answers

How to use a dot “.” to access members of dictionary with Python?

Sometimes, we want to use a dot "." to access members of dictionary with Python.

In this article, we’ll look at how to use a dot "." to access members of dictionary with Python.

How to use a dot "." to access members of dictionary with Python?

To use a dot "." to access members of dictionary with Python, we can use the dotmap library.

To install it, we run

pip install dotmap

Then we can use it by writing

from dotmap import DotMap

m = DotMap()
m.hello = 'world'
m.hello
m.hello += '!'
m.val = 5
m.val2 = 'Sam'

to create a DotMap object, which is a subclass of dict.

Then we assign values to properties of m.

And then we can use m.hello or m['hello'] to access properties of m.

We can convert m back to a regular dict with

d = m.toDict()

And we can convert a dict to a DotMap object with

m = DotMap(d) 

where d is a dict.

Conclusion

To use a dot "." to access members of dictionary with Python, we can use the dotmap library.

Categories
Python Answers

How to fix Python indentation?

Sometimes, we want to fix Python indentation.

In this article, we’ll look at how to fix Python indentation.

How to fix Python indentation?

To fix Python indentation, we can use the reindent.py script in the Python’s Tools/scripts/ folder.

If it’s missing, we run

pip install reindent

to install it.

Then we run

reindent foo.py

to fix the indentation in the foo.py file.

Conclusion

To fix Python indentation, we can use the reindent.py script in the Python’s Tools/scripts/ folder.

Categories
Python Answers

How to convert list to string with Python?

Sometimes, we want to convert list to string with Python.

In this article, we’ll look at how to convert list to string with Python.

How to convert list to string with Python?

To convert list to string with Python, we can use the string join method.

For instance, we write

list1 = ['1', '2', '3']
str1 = ''.join(list1)

to call the empty string’s join method with list list1 to return a string that has all the strings in list1 combined into a string.

Conclusion

To convert list to string with Python, we can use the string join method.