Friday, June 1, 2018

Reading a file line by line into elements of an array in Python

In python you can use the readlines method of a file object.



with open('topsites.txt') as f:
testsite_array=f.readlines()



or simply use list, this is same as using readlines but the only difference is that we can pass an optional size argument to readlines :



with open('topsites.txt') as f:
testsite_array=list(f)


help on file.readlines:




In [46]: file.readlines?
Type: method_descriptor
String Form:
Namespace: Python builtin
Docstring:
readlines([size]) -> list of strings, each a line from the file.

Call readline() repeatedly and return a list of the lines so read.
The optional size argument, if given, is an approximate bound on the
total number of bytes in the lines returned.

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...