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


Messages In This Thread
Text File Manipulation Queries? - by JustJeff - Apr-10-2021, 04:56 PM
RE: Text File Manipulation Queries? - by ndc85430 - Apr-10-2021, 05:56 PM
RE: Text File Manipulation Queries? - by JustJeff - Apr-10-2021, 08:12 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Too many queries? lorasf 6 1,198 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,284 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,892 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 7,428 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,585 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,698 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,545 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,251 Aug-09-2020, 02:12 PM
Last Post: snippsat
  Convert Excel file to Text file marvel_plato 6 20,487 Jul-17-2020, 01:45 PM
Last Post: marvel_plato
  Python re.sub text manipulation on matched contents before substituting xilex 2 2,246 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