-1

I need help importing data like this from a text file:

Orville Wright 21 July 1988

Rogelio Holloway 13 September 1988

Marjorie Figueroa 9 October 1988

and display it on the python shell like this:

Name

  1. O. Wright
  2. R. Holloway
  3. M. Figueroa

Birth date

  1. 21 July 1988
  2. 13 September 1988
  3. 9 October 1988
Zee Dhlomo
  • 69
  • 1
  • 1
  • 9
  • 1
    What did you do? You can use `str.split(' ')` to generate a list from your string. – Adrien Logut May 07 '18 at 18:44
  • 2
    We're not going to write this code for you. There are already questions on [how to read a text file into python](https://stackoverflow.com/questions/14676265/how-to-read-text-file-into-a-list-or-array-with-python?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa), [parsing unusual datetime formats](https://stackoverflow.com/questions/43786415/python-convert-unusual-date-string-to-datetime-format?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa), and [documentation on string manipulation](https://docs.python.org/3.4/library/string.html) – ChootsMagoots May 07 '18 at 18:47
  • I'm assuming you know how `print()` statements work. – ChootsMagoots May 07 '18 at 18:47
  • Take a look to the python documentation, you will find a way for reading files, then find a way to split the string as you need to: https://docs.python.org/2.7/tutorial/inputoutput.html?highlight=open#methods-of-file-objects – ricardoorellana May 07 '18 at 18:53
  • I am convinced that the documentation i get from my online course leaves information out. I've been struggling to figure out how to do this for two days. I've only gotten as far as splitting line by line... but I don't know how to manipulate the elements after splitting the data by lines... – Zee Dhlomo May 07 '18 at 18:58

1 Answers1

-1

Read lines of files into a list In Python, how do I read a file line-by-line into a list?

Enumeration https://docs.python.org/2.3/whatsnew/section-enumerate.html

with open('filename') as f:
  lines = f.readlines()  # see above link
  names = []  # list of 2-element lists to store names
  timestamps = []  # list of 3-element lists to store timestamps as day/month/year 

  # preprocess 
  for line in lines:
    a = line.split(" ")  # the delimiter you use appears to be a space
    names.append(a[:2])  # everything up to and excluding third item after split
    timestamps.append(a[2:])  # everything else

  # output
  print("some header here") # put whatever you want here
  for i, name in enumerate(names):  # see enumeration reference
    # you could add a length check on name[0] in case first name is blank
    print("{}. {}. {}".format(str(i+1), name[0][0], name[1]))  
  print("another header here") # again use whatever header you want here
  for i, timestamp in enumerate(timestamps):
    print("{}. {}".format(str(i+1), " ".join(timestamp))  
user1258361
  • 1,133
  • 2
  • 16
  • 25