0

The code is quite short so I put it here:

prog = []
# Reading the database from a file
try:
    with open('prog.txt') as f:
        progA = f.read().splitlines()
    for n in range(len(progA)/2):
        num = n*2
        prog.append([progA[num],progA[num+1]])
except:
    print "Something went wrong"
ch = 1
while True:
    nR = True
    print prog[ch-1][0]
    # Getting input
    while nR:
        inp = raw_input(">>> ")
        try:
            inp = int(inp)
            if str(inp) in prog[ch-1][1]:
                ch = inp
                nR = False
            else:
                print "You can't move to that channel"
        except:
            print "That isn't a number"

And the contents of the file (prog.txt):

DATABASE\nBrowse by entering the number you see after the link.\n> VERSION (3)
23
VERSION\nThis is the latest version.\n> Main menu (1)
1

The code works, but not as I wanted. It prints the newlines as it would be any other text, not with an actual new line.

1
  • \n in prog.txt is treated as normal character, thats why! Commented Aug 20, 2015 at 17:42

2 Answers 2

1

If you want newline characters in your database file, use actual newline characters, created with the Enter key. If your file contains the character \ followed by the character n, those characters will be interpreted literally. Either create a database file with actual newlines, or preprocess it to replace the literal \n with an actual newline, with .replace('\\n', '\n').

Sign up to request clarification or add additional context in comments.

Comments

0

The \n is not converted to a real newline when you read it from a file. Compare

>>> print "foo\nbar"
foo
bar
>>> print raw_input()
foo\nbar
foo\nbar

\n is something that is processed by Python when creating str objects from string literals.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.