TypeError: coercing to Unicode: need string or buffer when accessing a file

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
thirstyoctopus
Regular
Posts: 61
Joined: Mon Aug 27, 2018 12:04 pm
Projects: Romance Visual Novel story
Organization: Thirsty Octopus
Contact:

TypeError: coercing to Unicode: need string or buffer when accessing a file

#1 Post by thirstyoctopus » Sun Apr 14, 2019 3:02 pm

This issue has become too much for me now, after research I still cannot find an answer that really solves it.

What I'm trying to do:
At the point where the player is asked their name, I want to filter the input to warn the player when he/she types a rude word or combination of rude words stipulated in an external file bannedwords.txt.

How I do it:
Here's the section of code that defines the function which will decide if a banned word or words have been entered (I actually got a lot of this code from another post, as someone else wanted the same thing):

Code: Select all

python:
            import collections

            def ContainsBannedWord(name):
                # Get list of bad words from a text file, each word on a separate line
                bannedWordsList = [line.rstrip('\n') for line in open(renpy.file("../bannedwords.txt"), "r")]

                # Check if the name contains a bad word
                for bannedWord in bannedWordsList:
                  if (bannedWord in name):
                    return True
I then call the input screen:

Code: Select all

$ player_name = renpy.call_screen("input", prompt="Tell me your name:")
Which calls:

Code: Select all

screen input(prompt):
    modal True

    zorder 200

    style_prefix "confirm"

    add "gui/overlay/confirm.png"

    frame:
        vbox:
            xalign .5
            yalign .5
            spacing 30

            text prompt
            input id "input" color gui.input_text_color
Then, I try to check against the entered word / name to see if it contains any of the 'banned' words, and then return another instance of the same screen with a different prompt:

Code: Select all

if (ContainsBannedWord(player_name.lower().strip())):
            $ player_name = renpy.call_screen("input", prompt="Come on, don't be rude to me!")
What happens when the function is called:
I get the following error in Ren'py:

Code: Select all

While running game code:
  File "game/scripts/mc_scenes/outside/outside1.rpy", line 121, in script
    if (ContainsBannedWord(player_name.lower().strip())):
  File "game/scripts/mc_scenes/outside/outside1.rpy", line 121, in <module>
    if (ContainsBannedWord(player_name.lower().strip())):
  File "game/scripts/mc_scenes/outside/outside1.rpy", line 112, in ContainsBannedWord
    bannedWordsList = [line.rstrip('\n') for line in open(renpy.file("../bannedwords.txt"), "r")]
TypeError: coercing to Unicode: need string or buffer, file found
I discovered that this could mean that I'm trying to pass an integer to a string or vice versa but it really doesn't make sense to me. The bannedwords.txt file is literally a plain text file with a list of words I don't want the player to use.

Can anyone shed some light on what I'm doing wrong here?

User avatar
SleepKirby
Veteran
Posts: 255
Joined: Mon Aug 09, 2010 10:02 pm
Projects: Eastern Starlight Romance, Touhou Mecha
Organization: Dai-Sukima Dan
Location: California, USA
Contact:

Re: TypeError: coercing to Unicode: need string or buffer when accessing a file

#2 Post by SleepKirby » Sun Apr 14, 2019 4:06 pm

As a general debugging tip - when an error happens on a complex line like this:

Code: Select all

bannedWordsList = [line.rstrip('\n') for line in open(renpy.file("../bannedwords.txt"), "r")]
I'd prefer to temporarily break it up into multiple simpler lines, then run the code again to see which line gets the error:

Code: Select all

renpyFileResult = renpy.file("../bannedwords.txt")
openedFile = open(renpyFileResult, "r")
bannedWordsList = [line.rstrip('\n') for line in openedFile]
Looking at the documentation for renpy.file(), I'm guessing the second line will get the error, because renpy.file() should already return an open file. In fact, renpy.file() is supposed to do almost the same thing as open(), with the main difference being that it's better at finding files within a Ren'Py project. The error happens because open() expects a filename or filepath (a string) as its first argument.

A demonstration of the error message in regular Python 2.7:

Code: Select all

>>> f = open('C:/my_text_file.txt', 'r')
>>> open(f, 'r')

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    open(f, 'r')
TypeError: coercing to Unicode: need string or buffer, file found

User avatar
thirstyoctopus
Regular
Posts: 61
Joined: Mon Aug 27, 2018 12:04 pm
Projects: Romance Visual Novel story
Organization: Thirsty Octopus
Contact:

Re: TypeError: coercing to Unicode: need string or buffer when accessing a file

#3 Post by thirstyoctopus » Sun Apr 14, 2019 4:09 pm

I see. So, I assume it's better to use renpy.file() instead of open() then? But, obviously, not both.

User avatar
SleepKirby
Veteran
Posts: 255
Joined: Mon Aug 09, 2010 10:02 pm
Projects: Eastern Starlight Romance, Touhou Mecha
Organization: Dai-Sukima Dan
Location: California, USA
Contact:

Re: TypeError: coercing to Unicode: need string or buffer when accessing a file

#4 Post by SleepKirby » Sun Apr 14, 2019 4:12 pm

Indeed, I'd try using renpy.file() without open().

Post Reply

Who is online

Users browsing this forum: Google [Bot]