2

I am a beginner with python and I am unsure what I should be searching for with regards to my assignment. I am trying to find a way to manipulate a text file to the output format required.

The question is as follows: "Write a program that reads the data from the text file called ‘DOB.txt’ and prints it out in two different sections."

DOB text file reads (Name, Surname and DOB per line):

Orville Wright 21 July 1988
Rogelio Holloway 13 September 1988
Marjorie Figueroa 9 October 1988
Debra Garner 7 February 1988
Tiffany Peters 25 July 1988
Hugh Foster 2 June 1988
Darren Christensen 21 January 1988
Shelia Harrison 28 July 1988
Ignacio James 12 September 1988
Jerry Keller 30 February 1988

Pseudo code as follows:

Read the file DOB.txt

create a loop that will go over the contents being read from the File

display every line being read ( this will have the name and the DOB of the 
students)

Use string indexing to get the first name and the 2nd name 

assign this to a variable and print the first name and surname

For example something like this first_name = name[0] + name[1]

Keep in mind the name is the iterator that will be looping over the file we 
are reading too.

DO the same to the DOB ( use string indexing to get the DOB of the student )

Counter for numbering each name

counter for numbering each surname

open file for reading

store contents of file in the form of lines

loop through contents

split each line into words 

access the word and charcaters using indexing

Expected results are as per below:

Name:

  1. Orville Wright
  2. Rogelio Holloway
  3. Marjorie Figueroa
  4. Debra Garner
  5. Tiffany Peters

Birth Date:

  1. 21 July 1988
  2. 13 September 1988
  3. 9 October 1988
  4. 7 February 1988
  5. 25 July 1988
Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63

2 Answers2

0

This isn't a place to ask about your homework, especially without showing what you've done already and/or showing what errors you're having a hard time dealing with.

Some tips for the assignment:

  • Opening and closing files in Python. I suggest scrolling down to the end of the section and reading about the with keyword. Example:

    with open("file.txt", "r") as f:
        do_something_to_file()
    

    instead of :

    f = open("file.txt", "r")
    do_something_to_file()
    f.close()
    
  • Splitting strings. Since you're reading the whole line as a string from a text file, you'll need to split it into first names, surname, DOB, etc. Definition of str.split() from the PyDocs. split() returns a list of the elements, separated by the character you passed, defaults to whitespace.

    >>> string = "hello world"
    >>> string.split()
    ['hello', 'world']
    

    You can also specify a maximum number of elements to be split from the string. To set a maximum of 3 elements:

    >>> string = "Today was a good day"
    >>> string.split(" ", 2)
    ['Today', 'was', 'a good day']
    
  • You don't need to store a counter for something as simple as what you describe in the question. When iterating over the list of names to print, you can use enumerate. enumerate returns an object containing each of the elements of the iterable (in your case a list) you pass it, along with a count, starting from 0 by default. Example:

    >>> word_list = string.split(" ", 2)
    >>> for i, word in enumerate(word_list, 1):
    ...     print(f"{i}. {word}")
    ...
    1. Today
    2. was
    3. a good day
    

I would suggest adding code into your question by editing it. You can see examples of how to format your question here.

Good luck in your assignment.

Alex
  • 509
  • 4
  • 8
  • Hi , I did add a note that has subsequently been deleted : it read that I have tried to add my code but every time I did it said it wasn't a valid format. I'm coping straight outta my VS Code . – Nicole_9026 Aug 29 '19 at 12:12
  • Attempt 1 f = open("DOB.txt", "r") for line in f: name = line(name[0:""]) print(name) dob = line([2"":]) print(dob) # Attempt 2 import sys f = open('DOB.txt', 'r') for line in f: txt = line.strip() if txt == '': sys.stdout.write('\n\n') sys.stdout.flush() sys.stdout.write( txt + ';') sys.stdout.flush() f.close() – Nicole_9026 Aug 29 '19 at 12:12
  • the above is what stack overflow does and I don't know why – Nicole_9026 Aug 29 '19 at 12:13
  • I added some code examples to make things easier. To add code blocks you just need to have 4 empty spaces at the start of each line. It would make it a lot simpler if we could see what you wrote so far. – Alex Aug 29 '19 at 12:19
0

Not sure if I fully understood what your assignment wants from you but what I got is:

  1. You need to parse over this file's lines
with open('dob.txt') as dob:
    for row in dob:
        #print(row) would show you you're actually printing the file's rows
  1. You'll probably want to split the words in this row to a list. I named the list 'rowstrings'
with open('dob.txt') as dob:
    for row in dob:
        rowstrings = row.split()
  1. If you try printing the 'rowstring' list's elements you'd see that on every line name and surname elements are at this list positions 0 and 1, and the DOB elements are at positions 2, 3 and 4.
with open('dob.txt') as dob:
    for row in dob:
        rowstrings = row.split()
        name = rowstrings[0] + " " + rowstrings[1]
        birthdate = rowstrings[3] + " " + rowstrings[2] + " " + rowstrings[4]
  1. To have the counters I'd simply create the variables outside the loop and do this:
namecounter = 0
surnamecounter = 0
with open('dob.txt') as dob:
    for row in dob:
        rowstrings = row.split()
        name = rowstrings[0] + " " + rowstrings[1]
        birthdate = rowstrings[3] + " " + rowstrings[2] + " " + rowstrings[4]
        namecounter += 1
        surnamecounter += 1

To print the name and birthdate of all rows in your file the print statement needs to be inside the for loop.

namecounter = 0
surnamecounter = 0
with open('dob.txt') as dob:
    for row in dob:
        rowstrings = row.split()
        name = rowstrings[0] + " " + rowstrings[1]
        birthdate = rowstrings[3] + " " + rowstrings[2] + " " + rowstrings[4]
        namecounter += 1
        surnamecounter += 1
        print("Name\n" + name + "\n\n" + "Birth date\n" + birthdate)

Since it looks like you want to first print the names and then print the birth dates you could create two empty lists and fill in one of these lists with the names and the other with the birth dates. You could then loop over the lists.

namelist = []
birthlist = []
namecounter = 0
surnamecounter = 0
with open('dob.txt') as dob:
    for row in dob:
        rowstrings = row.split()
        name = rowstrings[0] + " " + rowstrings[1]
        birthdate = rowstrings[3] + " " + rowstrings[2] + " " + rowstrings[4]
        namecounter += 1
        surnamecounter += 1
        namelist.append(name) #adds the string to the list
        birthlist.append(birthdate)

for row in namelist: #prints each name in the namelist
   print(row)

for row in birthlist: #prints each birthdate in the birthlist
   print(row)
otus666
  • 72
  • 7