Python Forum

Full Version: Text File Manipulation Queries?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm a new python learner and I've been looking at several different sources to understand how python works. Today I decided to challenge myself by making a problem which stores user input on several aspects of a desired phone on a text file, using the concept of classes. However, I'm slightly annoyed at how the information is presented on the text file. At the moment it presents the information in one straight line like so:
Brand: Samsung.Model: Samsung Galaxy A40.Year Produced: 2019.Information Provided By Jeffrey
However, I want it to be presented like so:
Brand: Samsung.
Model: Samsung Galaxy A40
Year Produced: 2019
Information Provided By Jeffrey
Any suggestions?
My code:
#This program recieves phone data input from the user, and stores it in an array (within the class). It then adds it to
#a text file.
class phoneData:
    def __init__(self, brand, modelName, yearProduced):
        self.brand = brand
        self.modelName = modelName
        self.yearProduced = yearProduced
    def printstoredResult(self):
       print("The " + self.modelName + " was made in " + self.yearProduced + " by " + self.brand)
try:
   file = open("phoneData.txt")
except:
    print("No such file exists")
    file = open("phoneData.txt", "x")
    file.close()
finally:
    name = input("Please Enter Your Name")
    print("Hello " + name + "! Welcome To The PDIS (Phone Data Input Service). We store information about phones in our data base for reference!")
    userInput = input("Would you like to input some phone information? Answer with yes or no.")
    filteredInput = userInput.replace(" ", "")
    if filteredInput == "yes" or filteredInput == "Yes":
        brand = input("Alright. Please enter the desired phone brand.")
        newBrand = brand.replace(" ", "")
        modelName = input("That's cool! A Phone Made By " + newBrand + "! Enter the model name now!")
        yearProduced = input("Finally, enter the year it was produced in.")
        filteredYear = yearProduced.replace(" ", "")
        print("That's great!")
        newPhone = phoneData(newBrand, modelName, filteredYear)
        newPhone.printstoredResult()
        file = open("phoneData.txt", "a")
        file.write("Brand: " + newPhone.brand + "."
                   "Model: " + newPhone.modelName + "."
                   "Year Produced: " + newPhone.yearProduced + "."
                   "Information Provided By " + name)




    else:
        print("Oh ok! Goodbye!")
You aren't writing any new line characters, so of course everything appears on one line. At the very least, you can include the new line characters for your platform in the string (e.g. \n on UNIX, not sure for Windows. There should be something in the standard library that gets you the platform specific new line characters, though). There may be a method on the file object that will let you pass a list or something and will write the lines for you (I thought it was called writelines, but couldn't find it in the docs).
Ok I'll try that then. Never heard of new line characters though so I'll look them up