0

data.txt file:

Lambert 34 10.50
Osborne 22 6.25
Giacometti 5 100.70
f = input("Enter the file name: ")

f1 = open(f, 'r')

print("Name"  ,   "Hours",   "Total Pay")

for line in f1:
    print(line)

current output:

Enter the file name: data.txt
Name Hours Total Pay
Lambert 34 10.50

Osborne 22 6.25

Giacometti 5 100.70

The issue with this code is that it needs to be printed out in tabular format. With headers of name, hours, total pay, with the contents of the file under it.

I know how to open the file, and print it by line, but I have no idea how I am going to make it like a table format. An example of the output is going to be like this:

Enter the file name: data.txt

Name            Hours      Total Pay
Lambert            34         357.00
Osborne            22         137.50
Giacometti          5         503.50

How would I do this? I'm almost there, I'm pretty sure I need to make my first print statement longer, and make an array, such as this from another stack overflow post

data = np.array([[1, 2, 1],
                 [0, 1, 0],
                 [2, 4, 2]])

But the issue is how would I make an array from contents from a text file?

Thank you

  • 4
    Use `pandas`. `pandas.read_csv('data.txt ', delim_whitespace=True)` should work. – rpanai Jan 24 '20 at 18:35
  • 1
    Did you loot into reading it as a CSV (using `\t` as your separator, it it is tab separated)? – robotHamster Jan 24 '20 at 18:35
  • Does this answer your question? [Printing Lists as Tabular Data](https://stackoverflow.com/questions/9535954/printing-lists-as-tabular-data) e.g. [this solution using a Pandas dataframe](https://stackoverflow.com/a/9536060/4518341) – wjandrea Jan 24 '20 at 18:51
  • @wjandrea please see the last line of his question he is asking for how to make an array from a text file. – i_am_deesh Jan 24 '20 at 19:02
  • @google Yes, a dataframe stores its data in an ndarray, plus it supports the column headers OP wants. Either way OP seems to be ultimately asking how to print a table - an ndarray is just one way of doing that. – wjandrea Jan 24 '20 at 20:36
  • @google Oh I see what you're saying - the question I linked doesn't explain how to convert the file to an array. I retracted my close vote. – wjandrea Jan 24 '20 at 21:30
  • What have you tried? What is the issue, exactly? – AMC Jan 24 '20 at 22:44

3 Answers3

1

I have modified the code of @rpanai to add columns name to suit your answer

df = pd.read_csv('data.txt', delim_whitespace=True, names=['Name', 'Hours', 'Total Pay'])
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Shubham Shaswat
  • 1,250
  • 9
  • 14
0
 import numpy as np
 data = np.genfromtxt("data.txt")

you can do this

if you want to skip the initial lines import numpy as np data = np.genfromtxt("data.txt")

or you can use pandas as well for this purpose

import pandas as pd
data = pd.read_csv("data.txt", sep=" ")

this should also work

i_am_deesh
  • 448
  • 3
  • 12
0

This is how I would go about it :

filename = input("Enter the file name: ")

items = []
try :
    with open (filename, 'r') as datafile :
        print("Name", "          Hours", "    Total Pay")
        for line in datafile :
            items = line.strip ('\n').split (' ')
            tab1 = ' ' * (18 - (len (items [0]) + len (items [1])))
            tab2 = ' ' * (12 - len (items [2]))
            print (items [0], tab1, items [1], tab2, items [2])
except OSError :
        print ('File ERROR occured.')
        print (filename, ' has not been loaded.')

Screen shot of output

bashBedlam
  • 1,402
  • 1
  • 7
  • 11