Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to read a text file and count how many lists it contains?

I’m writing a program which is converting, sorting and generally doing automate tasks.

My problem is now: I don’t know how to count how many lists a text file contains.

The Format in the text file is like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

["apple", "banana", "cherry"]

["apple", "banana", "cherry"]

I tried already some options with len() but only get the number of letters.

>Solution :

if each line contains a different list then you can count lines, by using simply

file = open("filename.txt", "r")
len(file.readlines())

otherwise if you want to read and store each list into a different list then you can do something like this

file  = open("input.txt", "r")
lists = []
for line in file.readlines():
  lst = line.replace("[", "").replace("]","").replace("\"","").replace("\n","").split(", ")
  lists.append(lst)
print(len(lists))    
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading