Python Forum

Full Version: re.findall HELP!!! only returns None
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
OK, I'm really new to python. I've only been learning for about two weeks now. I'm taking an online class to learn but I'm having a lot of trouble with one assignment. I'm coming here because I think it might be something other than y code because its really simple and I have even seen people successfully compile pretty much the exact same code.So im trying to find all the numbers in the string bellow using the re.findall() "function". I think my code works but every time I upload it regardless of any changes I make the output always comes back as 'None'. I don't know what to do and its been 2 days. Help please.

import re

y = 'We four guys, live at 2nd street of Malibeu. I had a cash of $248 in my pocket. I got a ticket with serial number 88796451-52.'
x = re.findall('[a-z]', y)
print(x)
And the output is just None not even a compiling error it just prints None
For me, it shows
Output:
['e', 'f', 'o', 'u', 'r', 'g', 'u', 'y', 's', 'l', 'i', 'v', 'e', 'a', 't', 'n', 'd', 's', 't', 'r', 'e', 'e', 't', 'o', 'f', 'a', 'l', 'i', 'b', 'e', 'u', 'h', 'a', 'd', 'a', 'c', 'a', 's', 'h', 'o', 'f', 'i', 'n', 'm', 'y', 'p', 'o', 'c', 'k', 'e', 't', 'g', 'o', 't', 'a', 't', 'i', 'c', 'k', 'e', 't', 'w', 'i', 't', 'h', 's', 'e', 'r', 'i', 'a', 'l', 'n', 'u', 'm', 'b', 'e', 'r']
Do you know why. Im still just as confused.
Know why what?

re.findall will never return None. If nothing is found it will return [], but there is no way you can make it return None. Therefore your problem is not that you run the code you provide and it prints "None".

How are you running this script? You say "I think my code works but every time I upload it". What do you mean by "upload it"?
post your code verbatim. you speak of function in your original post, but there is no function in the code you show.
Also note that [a-z] patterns will not give you "all the numbers" anyway. Yo may use site like 101regex.com to experiment with RegEx
Im running it through PyCharm and I'm coming from a c Arduino background so I didn't mean upload. And I was just experimenting with [a-z] and no matter what I put there it just outputs the word None. Thats what Im having trouble with. Its not even showing [] just the word None. Whats a verbatim? Is there somthing I have to download like a library for 'import re' or can I just call it.
verbatim means to show your code exactly as it is in pycharm. maybe even show a screenshot.
re module is part of Standard library, so no need to install anything
My guess is you wrote a function that uses regex and your function doesn't have a return. That would be easy to verify is you POSTED THE CODE instead of some different code that maybe includes some of the same words
Since Im brand new to the forum and this is my first post I am not allowed to add attachments according to the forums rules. But again I will show exactly what my compiler is running maybe if someone could try running it on heir setup that would be great.
import re

y = 'We four guys, live at 2nd street of Malibeu. I had a cash of $248 in my pocket. I got a ticket with serial number 88796451-52.'
x = re.findall('[a-z]', y)
print(x)
Output:
C:\Users\h************\PycharmProjects\untitled4\venv\Scripts\python.exe C:/Users/h************/Desktop/PycharmProjects/regular_expressions_example.py None Process finished with exit code 0

sorry I meant the error was.

Output:
C:\Users\h************\PycharmProjects\untitled4\venv\Scripts\python.exe C:/Users/h************/Desktop/PycharmProjects/regular_expressions_example.py None Process finished with exit code 0
(Jun-19-2020, 12:24 AM)Rusty Wrote: [ -> ]if someone could try running it on heir setup that would be great.

We did. @pyzyx3qwerty show the result already in post#2. Here again (just the string is shorter):

>>> import re
>>> y = 'We four guys, live at 2nd street of Malibeu'
>>> x = re.findall('[a-z]', y)
>>> print(x)
['e', 'f', 'o', 'u', 'r', 'g', 'u', 'y', 's', 'l', 'i', 'v', 'e', 'a', 't', 'n', 'd', 's', 't', 'r', 'e', 'e', 't', 'o', 'f', 'a', 'l', 'i', 'b', 'e', 'u']
As @deabhystad explained we suspect you have slightly different code and print None returned from a function. Especially because you mention function in your original post
Pages: 1 2