Categories
Python Answers

How to read first N lines of a file with Python?

Sometimes, we want to read first N lines of a file with Python.

In this article, we’ll look at how to read first N lines of a file with Python.

How to read first N lines of a file with Python?

To read first N lines of a file with Python, we can use the next function.

For instance, we write

with open("datafile") as myfile:
    head = [next(myfile) for x in range(N)]
print(head)

to call open to read the datafile file.

Then we call next with the myfile file to read the first N lines by putting next in the for loop.

Conclusion

To read first N lines of a file with Python, we can use the next function.

Categories
Python Answers

How to bin data in Python with scipy or numpy?

Sometimes, we want to bin data in Python with scipy or numpy.

In this article, we’ll look at how to bin data in Python with scipy or numpy.

How to bin data in Python with scipy or numpy?

To bin data in Python with scipy or numpy, we can use the linspace method to create the bins.

And then we call digitize to put the data into the bins`.

For instance, we write

import numpy
data = numpy.random.random(100)
bins = numpy.linspace(0, 1, 10)
digitized = numpy.digitize(data, bins)
bin_means = [data[digitized == i].mean() for i in range(1, len(bins))]

to create the bins with

bins = numpy.linspace(0, 1, 10)

We call linspace to creates with intervals of 0.1 between 0 and 1.

Then we put the data items into the bins with

digitized = numpy.digitize(data, bins)

And we get the means of the values in each bin with

[data[digitized == i].mean() for i in range(1, len(bins))]

Conclusion

To bin data in Python with scipy or numpy, we can use the linspace method to create the bins.

And then we call digitize to put the data into the bins`.

Categories
Python Answers

How to reliably open a file in the same directory as the currently running script with Python?

Sometimes, we want to reliably open a file in the same directory as the currently running script with Python.

In this article, we’ll look at how to reliably open a file in the same directory as the currently running script with Python.

How to reliably open a file in the same directory as the currently running script with Python?

To reliably open a file in the same directory as the currently running script with Python, we can get the current folder of the script and then use open with it.

For instance, we write

__location__ = os.path.realpath(
    os.path.join(os.getcwd(), os.path.dirname(__file__)))
f = open(os.path.join(__location__, 'bundled-resource.jpg'))

to get the current location of the script with

__location__ = os.path.realpath(
    os.path.join(os.getcwd(), os.path.dirname(__file__)))

We get the current directory with getcwd.

And we get the relative path to the current script with

os.path.dirname(__file__)

Then we call os.path.realpath to get the real path from the joined path to get the path of the current directory.

Then we call open with the __location__ path we created.

Conclusion

To reliably open a file in the same directory as the currently running script with Python, we can get the current folder of the script and then use open with it.

Categories
Python Answers

How to get a list of unique dictionaries with Python?

Sometimes, we want to get a list of unique dictionaries with Python.

In this article, we’ll look at how to get a list of unique dictionaries with Python.

How to get a list of unique dictionaries with Python?

To get a list of unique dictionaries with Python, we can use dict comprehension.

For instance, we write

L = [
    {'id':1,'name':'john', 'age':34},
    {'id':1,'name':'john', 'age':34},
    {'id':2,'name':'hanna', 'age':30},
    ] 
uniques = list({v['id']:v for v in L}.values())

to get the unique dictionaries with

{v['id']:v for v in L}

which creates a dictionary with the key being the id value of the dicts in L.

And we set v to the dict with the given 'id' value.

Then we call values to get the values from the returned dict which has the unique dictionaries.

Conclusion

To get a list of unique dictionaries with Python, we can use dict comprehension.

Categories
Python Answers

How to do SFTP in Python?

Sometimes, we want to do SFTP in Python

In this article, we’ll look at how to do SFTP in Python.

How to do SFTP in Python?

To do SFTP in Python, we can use the fsspec library.

To install it, we run

pip install fsspec

Then we use it by writing

from fsspec.implementations.sftp import SFTPFileSystem
fs = SFTPFileSystem(host=host, username=username, password=password)

fs.ls("/")

with fs.open(file_name) as file:
    content = file.read()

to create a SFTPFileSystem object to connect to the host with the username and password.

Then we call ls to list the root directory of the server.

And then we call fs.open to open the file with the file_name.

Conclusion

To do SFTP in Python, we can use the fsspec library.