4

I recently bought a raspberry pi for many projects, one of which is reddit bots. I'm using PRAW to make the bot and it works perfectly on my desktop PC(Windows 8.1) but on my raspberry by (Raspbian) it doesn't seem to work. I've narrowed it down to these few lines in question.
Brief explanation of what I'm trying to do: Get the selftext of the post and split it by spaces into words then check if certain words are in the post.

text = submission.selftext.replace(","," ").encode("utf-8").lower().split()
for i in range (0, len(players)):
    player = players[i].lower()
    if player in text:
        print(player)

On my PC this works fine but on the Pi the if statement was never triggered even though I'm using the EXACT SAME POST for each of these tests.

If you are not familiar with PRAW, I'm splitting a string and looking for words inside it, where text is the array of words to look through and players is the dictionary.

Edit: The code does not produce any errors, it just doesn't trigger the if statement like it does on my desktop.

Edit #2: Seems that it works fine with hard-coded variable text and player. I left text hard-coded to include an entry from what should be in players but it would not find it which leads me to believe the error is in how I create the list players Below is the method that I use to create it.

def getPlayers():
    players = []
    with open("PlayerIDs.txt") as f:
        for line in f:
            players.append(line)
    return players

SOLUTION between Python 2 and 3 there were changes to how unicode and encoding in general worked and therefor the code would not run on python 2.x on my pi. I ended up installing python3 and using virtualenv to setup python 3 and it all works fine now :)

Chris
  • 2,435
  • 6
  • 26
  • 49
  • 2
    Are both the Pi and the PC running the same version of Python? What does `sys.version_info` show? I suspect that you're running Python 3 on the Pi and Python 2 on the PC. – Mark Dickinson Aug 02 '14 at 15:24
  • And it's a pretty simple statement. How about printing the variables in play? – keyser Aug 02 '14 at 15:26
  • @mark Pi is running 2.7.3 PC running 3.4.1, is searching through an array different in those versions? – Chris Aug 02 '14 at 15:27
  • @keyser orriginally it gave me an array of unicode strings, thus the `.encode("utf-8")` portion. Never had that issue on PC. EDIT: I have in fact printed all the variable and they are now the exact same on both. – Chris Aug 02 '14 at 15:27
  • 3
    This smells like a bytes / str / unicode issue: As @keyser says, I suggest printing out the *types* and values of `player` and `text` just before the `if` statement. – Mark Dickinson Aug 02 '14 at 15:29
  • Im new to python, Java is my strong language. How do you print the type of a variable? That's what I thought it was but figured the encode fixed it – Chris Aug 02 '14 at 15:30
  • 1
    `print(type(player))` – Mark Dickinson Aug 02 '14 at 15:31
  • @Mark text is a list, an item in text is a string, player is a string.... Unfortunately its what I expected – Chris Aug 02 '14 at 15:33
  • When I replace text with an array and player with an element in that array it works fine... – Chris Aug 02 '14 at 15:38
  • 2
    What are the *actual* types of `player` and `text[0]`? `string` is not a Python type! :-) (`str` and `unicode` are valid Python 2 types; `bytes` and `str` are valid Python 3 types) – Mark Dickinson Aug 02 '14 at 15:39
  • @Mark sorry they are 1str` I just associate that with string :/ EDIT: check edit #2 pease – Chris Aug 02 '14 at 15:41
  • If you used Notepad in Windows to create the `players` file, the default encoding is UTF-16, not UTF-8. – aruisdante Aug 02 '14 at 15:52
  • @aruisdante I used a python script to create it in the format of `name\n` would that be UTF-16 as well? – Chris Aug 02 '14 at 15:54
  • Try creating the file by running the script on the Pi and see if it works. If so, then yes, you have an encoding issue. – aruisdante Aug 02 '14 at 15:54
  • would I be able to just write a script that basically copies it? It took about 2 hours for the other one to complete the list. – Chris Aug 02 '14 at 15:56
  • In general, I suggest you have a read through [this](https://docs.python.org/2/howto/unicode.html) documentation on Unicode in Python. – aruisdante Aug 02 '14 at 16:00
  • thanks, I'll read that and see what I can do. – Chris Aug 02 '14 at 16:01

1 Answers1

2

One possible issue is that you are running different versions of Python. Try running the following code on both the raspi and the computer:

print (sys.version)

I'll bet that one will say "2.something" and one will say "3.something". This, if it happens, means that you are using incompatible-ish versions of Python. The pi comes with python 2 (the IDE with it is IDLE by default, and IDLE 3, an IDE for Python 3 also is preloaded in rasbian. Just run the script in the IDE that matches the version on your computer.

Also, I know that it sounds simple, but the pi has often unreliable network capabilities. Are you sure that is connecting to the Internet properly?

Hope this helps!

rocket101
  • 7,369
  • 11
  • 45
  • 64
  • Already determined version mis match in comments above, you are correct it is 2.x on the pi and 3.x on PC although I don't think that's the issue. If it is, how do I run it with IDLE 3 instead? I am sure the pi properly connects to the internet. I've printed the var `text` out and it was working. I have also hard-coded a value from `players` into text and it still didn't work. – Chris Aug 02 '14 at 16:14
  • I cannot give you enough up votes. There was a compatability issue. Python 2.x and 3.x handle unicode a lot differently – Chris Aug 02 '14 at 16:42
  • @duck Glad to hear it helped! Any questions still remaining, or has everything been sorted out? – rocket101 Aug 02 '14 at 18:21
  • unless you know how to run cron jobs with virtual environment, then I'm done here, thanks again – Chris Aug 02 '14 at 19:27