0% found this document useful (0 votes)
71 views

## Parsing A Data File (Python For Beginner) Somet...

Uploaded by

viliame vuetibau
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

## Parsing A Data File (Python For Beginner) Somet...

Uploaded by

viliame vuetibau
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

7/9/2021 ## Parsing A Data File (python For Beginner) Somet... | Chegg.

com

  Textbook Solutions Expert Q&A Study Pack Practice 

Find solutions for your homework Search

home / study / engineering / computer science / computer science questions and answers / ## parsing a data file (python for beginner) sometime…

Question: ## Parsing a Data File (python for beginner)


Sometimes data c… Post a question
Answers from our experts for your tough
homework questions

## Parsing a Data File (python for beginner)


Enter question
Sometimes data comes in as a structured format that you have to
break down and turn into records so you
can process them. CSV, or
comma-separated values, is a common standard for doing this.
Construct a program that reads in the following data file:

```

Continue to post
Ling,Mai,55900

Johnson,Jim,56500
19 questions remaining
Jones,Aaron,46000

Jones,Chris,34500

Swift,Geoffrey,14200

Xiong,Fong,65000

Snap a photo from your


Zarnecki,Sabrina,51500

```
phone to post a question
We'll send you a one-time download
Process the records and display the results formatted as a
table, evenly spaced, as shown in the example link
output.

### EXAMPLE OUTPUT


888-888-8888 Text me
```

Last First Salary


By providing your phone number, you agree to receive a one-tim
-------------------------
automated text message with a link to get the app. Standard
messaging rates may apply.
Xiong Fong $65,000

Johnson Jim $56,500

Ling Mai $55,900

Zarnecki Sabrina $51,500

Jones Aaron $46,000


My Textbook Solutions
Jones Chris $34,500

Swift Geoffrey $14,200

```
### CONSTRAINTS

- Write your own code to parse the data. Don’t use a CSV
parser.

- Use spaces to align the columns.

- Make each column one space longer than the longest


Fundament... Fundament... Principles of..
value in the column.

- **CHALLENGE 1** Format the salary as currency with dollar signs


and commas.
2nd Edition 1st Edition 9th Edition
- **CHALLENGE 2** Sort the results by salary from highest to
lowest. HINT: Use the sort function. View all solutions

Expert Answer

Anonymous answered this


Was this answer helpful? 1 0
374 answers

def sortBySalary(line):   #used to sort the records by


salary

   return int(line[2])   #returns the salary as


a integer

#stores each columns width

col1_width=0

col2_width=0

col3_width=0

data=[]   #stores each record

try:

   fp=open("input.data","r")   #opens the


file

   for line in fp:   #reads each line

      
line=line.replace("\n","")   #removes the new line
       line=line.split(",")  
#seperates the line at comma

       data.append(line)   #adds


the line to data

      

       #calculates each columns maximum


width

      
if(col1_width<len(line[0])):

          
col1_width=len(line[0])

      
if(col2_width<len(line[1])):

          
col2_width=len(line[1])

      
if(col3_width<len(line[2])):

          
col3_width=len(line[2])

https://www.chegg.com/homework-help/questions-and-answers/parsing-data-file-python-beginner-sometimes-data-comes-structured-format-break-tur… 1/3
7/9/2021 ## Parsing A Data File (python For Beginner) Somet... | Chegg.com
   fp.close()   #closes the file

 
  
Textbook Solutions Expert Q&A
   data.sort(key=sortBySalary)   #sorts the
data based on salary
Study Pack Practice 

   #header along with the appended spaces

   #in each column number of spaces is column width minus


length of string to be printed

   header_col1="Last"+"
"*(col1_width-4+1)  
   header_col2="First"+" "*(col2_width-5+1)

   header_col3="Salary"+" "*(col3_width-6+1)

  
print("{}{}{}".format(header_col1,header_col2,header_col3))  
#printing the header

  
print("-"*(col1_width+col2_width+col3_width+4))  
#printing the lines

   for line in data:   #accessing each line


in data

       #in each column number of spaces is


column width minus length of string to be printed
       col1=line[0]+"
"*(col1_width-len(line[0])+1)

       col2=line[1]+"
"*(col2_width-len(line[1])+1)

       #formating the number using


commas

       col3="{:,}".format(int(line[2]))+"
"*(col3_width-len(line[2])+1)  

      
print("{}{}${}".format(col1,col2,col3))   #printing each
record

except:   #if any error while handling file

   print("File Error")

Hide comments 

Comments

You commented
how about getting the highest wages to start with. Reverse of this output?

Leave a comment

Post comment

https://www.chegg.com/homework-help/questions-and-answers/parsing-data-file-python-beginner-sometimes-data-comes-structured-format-break-tur… 2/3
7/9/2021 ## Parsing A Data File (python For Beginner) Somet... | Chegg.com

  Textbook Solutions Expert Q&A Study Pack Practice 

Up next for you in Computer Science

Python programming.
Develop a script to
Please show your work. convert

between Hex
(Hexadecimal)

number and Decimal


See more questions
number Hex numbers for subjects you study
have

a base of 16 and the

See answer See answer

COMPANY

LEGAL & POLICIES

CHEGG PRODUCTS AND SERVICES

CHEGG NETWORK

CUSTOMER SERVICE

© 2003-2021 Chegg Inc. All rights reserved.

https://www.chegg.com/homework-help/questions-and-answers/parsing-data-file-python-beginner-sometimes-data-comes-structured-format-break-tur… 3/3

You might also like