To start I will mention that I am new to the Python Language and come from a networking background. If you are wondering why I am using Python 2.5 it is due to some device constraints.
What I am trying to do is count the number of lines that are in a string of data as seen below. (not in a file)
data = ['This','is','some','test','data','\nThis','is','some','test','data','\nThis','is','some','test','data','\nThis','is','some','test','data','\n']
Unfortunately I can't seem to get the correct syntax to count \n. So far this is what I have come up with.
num = data.count('\n')
print num
The above code is clearly wrong for the intended output since it returns 1 in relation to the test data, I would like to see it return 4 if possible. It only finds 1 of the '\n' being the last one in the example since it matches.
If I use the following code instead to find all of them it doesn't run due to syntax.
num = data.count (\n)
print num
I based this code from the following link, the example is the third one down.
http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/strings3.html
Is there anyway of accomplishing this or should I be looking at a different solution?
Any help is appreciated
Thank-you
list.count
is used to get the count of an item in list, but you need to do a substring search in each item to get the count as 4.Total number of items that contain
'\n'
:Sum of count of all
'\n'
s indata
: