Getting the number of "lists" returned by split() in python -
so in order different task wanted number of lists returned split() function file of commands. file, command.txt
has below entries:
ps -a free
and code came convert list , number of list is:
with open('command.txt', 'r') file: #for i, v in enumerate(file): #i can line counts. # pass #print i+1 line in file: word = line.split() print word print len(word)
the output of code is:
['ps', '-a']
2
['free']
1
rather want output 2. since word
has 2 lists, ['ps', '-a'] , ['free']. please suggest how can modify or come appropriate code.
as stated in comments above, sounds asking line count. since you're looping through file, add counter:
n = 0 open('command.txt', 'r') file: line in file: word = line.split() print word n += 1 print n
Comments
Post a Comment