0

First post and newbie to coding. Apologies if this is redundant. I've searched high and low and have yet to come across something specific to this particular issue.

I'm designing a choose your own adventure game using Python3. It's a bit heavy on text, so I wanted to keep the dialogue and other story aspects in a separate text file so that I can keep my code as lean and clean as possible. I've tried a few different tactics unsuccessfully, including an attempt at implementing a dictionary in the text file to use in the script like indices. I'm not discounting that tactic, but have moved on and here is where I'm currently at:

def intro():
    intro = open("try.txt", "r")
    print(intro.readlines(0))
intro()

Where 0 is the first line of text in the text file. I'm hoping that I can select individual lines to grab and display when navigating through the script through prompts, but the output includes square brackets around the bulk of the text, and backslashes before apostrophes. As an example: ['Tensions are rising on the high seas. There\'s trouble about.']

Is there any way to preclude the addition of escape characters and/or the square brackets in the printed output using this method?

Thanks in advance!

Celeborne
  • 3
  • 2
  • How come you're using `repr()`? So from your example it looks like you get a list with a single string in it when you execute `intro.readlines(0)`.. try `print(intro.readlines()[0])` Also, I'm not sure you want to use `f.readlines(0)` there, especially with a 0 value... – Todd Apr 16 '20 at 02:01
  • I found this similar to your situation : https://stackoverflow.com/a/493152/10798048 – Dhruv Agarwal Apr 16 '20 at 02:01

4 Answers4

1

The escaped characters you're seeing aren't coming from the file. What's happening is you get a list of strings when you invoke readlines():

>>> f = open("foo.txt", 'r')
>>> f.readlines()
['hello world!\n', 'hello world!\n', 'hello world!\n', 'hello world!\n', 'hello world!\n', 'hello world!\n', 'hello world!\n', 'hello world!\n', 'hello world!\n', 'hello world!\n']

That's what the function is returning when you call it. Then you're invoking repr() on the list, which is intended mostly to display objects as structured code-like text - not for displaying them as regular strings. It's useful in error messages, or log output, and that sort of thing.

To get a single line of text from a file after invoking getlines() - which returns all the lines as a list, you index it like any other list:

print(myfile.getlines()[0])

Using print(...) this way ^, you get the line content without any extra characters.

print(repr(myfile.getlines()[0]))

And, using print(...) this way ^, the quotes and escaped newlines etc. will be displayed:

So there's no file text codecs strangeness happening here. That's how it's supposed to work.

Todd
  • 4,669
  • 1
  • 22
  • 30
0

readlines reads all the lines from a file and returns an array that has one element for each line. Its argument is a size_hint, meant to indicate a probable line length; it does not indicate which line to read. If you want to access a particular element by index you have to use square brackets [0].

repr is used to get a string representation of an object. Since you're working with strings, it's unnecessary as a string's representation is just itself inside quotes.

What you need is most likely:

def intro():
    intro = open("try.txt", "r")
    print(intro.readlines()[0])
intro()
0

When you do print(repr(intro.readlines(0))) you're printing the whole list of strings, not just one of the strings. That's why you're seeing brackets and quotes around the string you want to print. I would try something like this

def intro():
    intro = open("try.txt", "r")
    line_list = intro.readlines()
    print(line_list[0])
intro()

For the backslash in front of the apostrophes, you could process each string in the line_list to try to remove it. I'm not totally sure why you're seeing those backslashes though.

def intro():
    intro = open("try.txt", "r")
    line_list = intro.readlines()
    new_line_list = []
    for line in line_list:
        new_line_list.append(line.replace("\\", ""))
    print(new_line_list[0])
intro()
Max Behling
  • 311
  • 1
  • 7
-1
def intro():
    intro = open("try.txt", "r")
    print(intro.readlines()[0]) # here

intro()