I seem to have having an issue with trailing newlines when reading in a file.
Steps to reproduce the problem:
(1. Create a file with newlines.
(2. Read in file as an array with readlines.
(3. Sample array with random.
(4. Print sample array to screen.
What seems to get printed is:
Hello World\n.
And not:
Hello World. I have to use static data ( directly hard coded data ) in order to use it.
My use case:
I tend to read version numbers from a file, and use that version number to choose an item from an array. I don't want a newline symbol also being printed.
The error is from readlines().
Is it an issue with random, or something else?
This seems to work fine though:
I'm more used to Ruby, where reading in a file doesn't cause the newline to actually show up. I read in files a lot for things like using version numbers to pick an item in an array.
Steps to reproduce the problem:
(1. Create a file with newlines.
(2. Read in file as an array with readlines.
(3. Sample array with random.
(4. Print sample array to screen.
What seems to get printed is:
Hello World\n.
And not:
Hello World. I have to use static data ( directly hard coded data ) in order to use it.
My use case:
I tend to read version numbers from a file, and use that version number to choose an item from an array. I don't want a newline symbol also being printed.
The error is from readlines().
Is it an issue with random, or something else?
This seems to work fine though:
# Import necessary modules.
import os
import random
import time
# Clear the screen
os.system("clear")
# Create a datasheet of pet varieties.
pets = [
"cat", "dog", "rat",
"gerbil", "hamster", "guiniepig",
"monkey", "snake",
]
# Create a datasheet of interior varieties.
interior = [
"bedroom", "closet", "bathroom",
"hallway", "kitchen", "living room",
"dining room", "garage",
]
# Create a datasheet of exterior varieties.
exterior = [
"outhouse", "driveway", "frontyard",
"backyard", "fence",
]
# Create pet string from pet sample stripping brackets.
sample_pets = random.sample(pets, k = 1)
active_pet = str(sample_pets).strip("[]''")
# Create interior string from interior sample stripping brackets.
sample_interior = random.sample(interior, k = 1)
active_interior = str(sample_interior).strip("[]''")
# Create exterior string from exterior sample_interior stripping brackets.
sample_exterior = random.sample(exterior, k = 1)
active_exterior = str(sample_exterior).strip("[]''")
# Display ai result symbolically.
print(active_pet + "(" + active_interior + ", " + active_exterior + ")\n")
file = open("data.txt", "w")
file.write("I have an animal in my" + active_interior +
". The animal within my house is my " + active_pet +
". Therefore, it is my " + active_pet +
" that is in my " + active_exterior +
".\n")
file.close()This performs as I expect it without the trailing newlines.I'm more used to Ruby, where reading in a file doesn't cause the newline to actually show up. I read in files a lot for things like using version numbers to pick an item in an array.
