+ 2
Count words in a file without counting special characters, numbers, spaces or individual letters
In operation B in my code if it should find the number of words without counting up any spaces or special characters, numbers or individual letters. If i test operation b in my code on my test file called breaktest which has special characters, spaces, numbers, individual letters, and one word, thus output should be 1, but its not, it counts the individual letters as words, making my output 4. Here is a pastebin link to my code please help https://pastebin.com/J6zw4pCm
11 Respostas
+ 1
Sousou if i enter an individual letter in the string, for example in the middle of the special characters, it still adds the individual character to the result
m="Hello.! #@+$+'*) World?'"
Has no individual letters in it, if i were to make it
m="Hello.! #@p+$l+'g*) World?'"
It adds the individual letters to the result, equalling
HelloplgWorld
+ 1
Thank you anyway for trying, this is frustrating hahaha
+ 1
gabi jo There's clearly 6 words in testfile.txt.
They are: "The", "dog", "walked", "across", "the", "room".
Am I missing something?
+ 1
Oof i need sleep ill try it again i thought i entered breaktest not testfile haha
0
maybe add another filter to check whether the character is more than 1. sorry not so familiar with python.
regex also able to do the trick
len(re.findall('([a-zA-Z]{2,})',yourString))
0
I have no understanding of the regex (re) stuff sadly, and when i try to add another filter i get error after error :(
0
letterfilter(only upper and lower letters):
https://code.sololearn.com/cLodGi04Oh3Z/?ref=app
0
ooooh yeah i didnt think about that case.
aaaaaa.... i keep forget to use and instead of && 😂
0
W_num = sum([len(i)>1 and i.isalpha() for i in [j.strip(string.punctuation) for j in F_Contents.split()]]) ended up with 6 as my word count in testfile but theres only 1 word :((( thank you though
0
Omg omg you were right thank you so much ive been stressing so much over this :)))))