Reputation: 3
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.
Upvotes: 0
Views: 60
Reputation: 530920
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.
Upvotes: 0
Reputation: 49320
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')
.
Upvotes: 1