Reputation: 13
I have a dictionary that contains multiple variables. The variables are defined by looking through a file and randomly selecting one of the lines. When I print the dictionary the strings have "\n" at the end.
import random as r
import math
FNameF = open("First-Names.txt")
LNameF = open("Last-Names.txt")
lines = FNameF.readlines()
lines2 = LNameF.readlines()
Name = (lines[r.randint(1,200) - 1])
LName = (lines2[r.randint(1,300) - 1])
Age = math.floor(abs(r.random() - r.random()) * (1 + 85 - 16) + 16)
people = {"person1" : {'Name': Name, 'Last Name': LName, 'Age': Age}
}
print(people["person1"])
The output is: {'Name': 'Wilson\n', 'Last Name': 'Terrell\n', 'Age': 61} What I want is: {'Name': 'Wilson', 'Last Name': 'Terrell', 'Age': 61} , removing the "\n"
Upvotes: 1
Views: 48
Reputation: 26
Try:
FNameF = open("First-Names.txt")
allLines = FNameF.read()
lines = allLines.split('\n')
Name = (lines[r.randint(1,200) - 1])
people = {"person1" : {'Name': Name, 'Last Name': LName, 'Age': Age}
}
print(people["person1"])
Upvotes: 1