Question

Write a Python program that reads from user the input file called data.txt that consists of...

Write a Python program that reads from user the input file called data.txt that consists of a  keyword followed by a sequence of numbers separated by commas. If the keyword is UP, the  numbers are sorted in increasing order, if the key word is DOWN, the numbers are sorted in  decreasing order. The sorted sequence of numbers are saved in sorted.txt

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

# Opening file handlers
fh = open('data.txt','r')
fh2 = open('sorted.txt','w')

# Iterating over each line of data.txt
for line in fh.readlines():
    content = line.split() # Converting data into list
    way = content[0] # Extracting the way the line to be sorted
    num = list(map(int,content[1:])) # Converting list into list of integers
    
    if way == 'UP':
        num.sort()
    if way == 'DOWN':
        num.sort(reverse = True)
    
    for n in num:
        fh2.write(str(n)+" ") # Wrting into the file
    fh2.write("\n")
    
fh.close()
fh2.close()

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Write a Python program that reads from user the input file called data.txt that consists of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a function in PYTHON that reads a sequence of numbers ended by 0 and prints...

    Write a function in PYTHON that reads a sequence of numbers ended by 0 and prints “Sorted” if the numbers were input in increasing or decreasing order, otherwise the program must print “Not sorted” (Do not calculate 0) Sample Input 4 3 -1 -5 0 Sample Output Sorted

  • Write a PYTHON program that reads a file (prompt user for the input file name) containing...

    Write a PYTHON program that reads a file (prompt user for the input file name) containing two columns of floating-point numbers (Use split). Print the average of each column. Use the following data forthe input file: 1   0.5 2   0.5 3   0.5 4   0.5 The output should be:    The averages are 2.50 and 0.5. a)   Your code with comments b)   A screenshot of the execution Version 3.7.2

  • In Python, write a program that reads a text file that is provided by the user...

    In Python, write a program that reads a text file that is provided by the user - ensure that the file exists before processing it! The file might be in a different directory, so be sure to test you logic - the user will provide the absolute path to the file if it is not in the same directory as the Python script! You should then ask the user for what string to search for in the file. Your program...

  • In Python 3, Write a program that reads in a text file that consists of some...

    In Python 3, Write a program that reads in a text file that consists of some standard English text. Your program should count the number of occurrences of each letter of the alphabet, and display each letter with its count, in the order of increasing count. What are the six most frequently used letters?

  • (statistics.py) Write a program that reads data from the file provided, data.txt, into a list. Once...

    (statistics.py) Write a program that reads data from the file provided, data.txt, into a list. Once the data are in a list, calculate and print the following: sum = 69.28 mean = 3.46 min = 0.19 max = 8.29 You may use the Python built-ins, min(), max(), and sum(). Format the numbers using '.2f'.

  • Write a Python program that converts an input file in FASTA format, called "fasta.txt", to an...

    Write a Python program that converts an input file in FASTA format, called "fasta.txt", to an output file in PHYLIP format called "phylip.txt". For example, if the input file contains: >human ACCGTTATAC CGATCTCGCA >chimp ACGGTTATAC CGTACGATCG >monkey ACCTCTATAC CGATCGATCC >gorilla ATCTATATAC CGATCGATCG Then the output file should be human ACCGTTATACCGATCTCGCA chimp ACGGTTATACCGTACGATCG monkey ACCTCTATACCGATCGATCC gorilla ATCTATATACCGATCGATCG FASTA format has a description (indicated with a '>') followed by 1 or more lines of a DNA sequence. PHYLIP format has a description...

  • (Count the occurrences of each keyword) Write a python program that reads in a Python source...

    (Count the occurrences of each keyword) Write a python program that reads in a Python source code file and counts the occurrence of each keyword in the file. Your program should prompt the user to enter the Python source code filename.

  • (Python 3) Write a program that reads the contents of a text file. The program should...

    (Python 3) Write a program that reads the contents of a text file. The program should then create a dictionary in which the keys are individual words found in the file and the values are the number of times each word appears and a list that contains the line numbers in the file where the word (the key) is found. Then the program will create another text file. The file should contain an alphabetical listing of the words that are...

  • Write a program that first reads in the name of an input file and then reads the file using the csv.reader() method.

     Write a program that first reads in the name of an input file and then reads the file using the csv.reader() method. The file contains a list of words separated by commas. Your program should output the words and their frequencies (the number of times each word appears in the file) without any duplicates. Ex: If the input is: inputl.csv and the contents of input1.csv are: hello, cat, man, hey, dog, boy, Hello, man, cat, woman, dog, Cat, hey, boy the output is: hello 1 cat 2 man...

  • Write a PYTHON program that asks the user for the name of the file. The program...

    Write a PYTHON program that asks the user for the name of the file. The program should write the contents of this input file to an output file. In the output file, each line should be preceded with a line number followed by a colon. The output file will have the same name as the input filename, preceded by “ln” (for linenumbers). Be sure to use Try/except to catch all exceptions. For example, when prompted, if the user specifies “sampleprogram.py”...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT