Python Forum
Text File Manipulation Queries?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Text File Manipulation Queries?
#1
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!")
Reply
#2
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).
JustJeff likes this post
Reply
#3
Ok I'll try that then. Never heard of new line characters though so I'll look them up
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Too many queries? lorasf 6 980 Jul-04-2023, 04:27 AM
Last Post: lorasf
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,114 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,658 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 6,961 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,328 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  Saving text file with a click: valueerror i/o operation on closed file vizier87 5 4,394 Nov-16-2020, 07:56 AM
Last Post: Gribouillis
  saving data from text file to CSV file in python having delimiter as space K11 1 2,397 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  Web Form to Python Script to Text File to zip file to web wfsteadman 1 2,133 Aug-09-2020, 02:12 PM
Last Post: snippsat
  Convert Excel file to Text file marvel_plato 6 19,666 Jul-17-2020, 01:45 PM
Last Post: marvel_plato
  Python re.sub text manipulation on matched contents before substituting xilex 2 2,112 May-19-2020, 05:42 AM
Last Post: xilex

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020