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:
["apple", "banana", "cherry"]
["apple", "banana", "cherry"]
I tried already some options with len()
but only get the number of letters.
CodePudding user response:
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))