Python Counting The Length Of A String By Needing To Split It Into A List And Separating Elements
My question involves finding sentences within a text which contain a semicolon, and finding the number of words before and after the semicolon. I understand how to split everything
Solution 1:
Not sure what your purpose is, but from what I understand you just want the number of words to the left of a semi and to the right of it on each line of a file? Correct? If so, this should work for ya.
with open(textfile,'rt',encoding='utf-8')as infile:
for line in infile:
for i,e in enumerate(line.split(' ')):
if e.endswith(';'):
print("--> {}\nContains {} words to the left, and {} words to the right\n".format(line.strip(),i+1,len(line.strip().split(" "))-(i+1)))
Post a Comment for "Python Counting The Length Of A String By Needing To Split It Into A List And Separating Elements"