Question

I'm having trouble reading a list of names from txt file, then printing them in alphabetical...

I'm having trouble reading a list of names from txt file, then printing them in alphabetical order.

Python 3, Windows

The names are listed in the text file in the following format:

name1

name2

name3

name4

The format print output should be (in alphabetical):

name1, name2, name3, name4

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

OUTPUT :

CODE :

#Open file to read names
f = open('names.txt')
#List to store names
names = []
#Read all lines in the file
for line in f:
#Add name to list
names.append(line.strip())
#Sort the names list
names.sort()
#print sorted names
print(','.join(names))

Add a comment
Know the answer?
Add Answer to:
I'm having trouble reading a list of names from txt file, then printing them in alphabetical...
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
  • I am working on a normal javascript program where i have to take two names and...

    I am working on a normal javascript program where i have to take two names and put them in array and print out in sorted order.But,when the user clicks the print button it should show the output,but if user clicks second time,it should start again(basically reload the page).I have done the part about userinput and sorting and printing,but couldn't get the function of when user clicks button second time. <!DOCTYPE html> <html> <body> <input type="text" id="Name1" value=""><br> <input type="text" id="Name2"...

  • Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed...

    Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed as command-line arguments to MergeFiles as follows: java MergeFiles filel file2 mergedFile II. MergeFiles reads names stored in files file and file2 III. Merges the two list of names into a single list IV. Sorts that single list V. Ignores repetitions VI. Writes the sorted, free-of-repetitions list to a new file named mergedFile.txt VII. The names in all three files are stored as one...

  • I am having trouble trying to output my file Lab13.txt. It will say that everything is...

    I am having trouble trying to output my file Lab13.txt. It will say that everything is correct but won't output what is in the file. Please Help Write a program that will input data from the file Lab13.txt(downloadable file); a name, telephone number, and email address. Store the data in a simple local array to the main module, then sort the array by the names. You should have several functions that pass data by reference. Hint: make your array large...

  • I am creating a program to sort a list of names from a given text file...

    I am creating a program to sort a list of names from a given text file using linked listed. The text file would look something like this: 3 John Johnson 1 Tim Boring 1 Jason Mendoza 1 Bobert Reed 2 Tim Boring 4 5 Essentially I would like to associate each numerical value with an action: 1 = add a person, 2 = remove person, 3 = check if that name is in the list, 4 = print list, 5...

  • I'm needing help writing a program that reads a list of scores from a .txt file...

    I'm needing help writing a program that reads a list of scores from a .txt file then drops the lowest. Instructions are below. Then use the "low score drop" algorithm in a program to read and process an indeterminate number of floating-point scores from a file. The program should then display the result (the lowest score in the file), with two digits after the decimal. There is no restriction on these values; they might be negative or positive, and the...

  • The following code uses a Scanner object to read a text file called dogYears.txt. Notice that...

    The following code uses a Scanner object to read a text file called dogYears.txt. Notice that each line of this file contains a dog's name followed by an age. The program then outputs this data to the console. The output looks like this: Tippy 2 Rex 7 Desdemona 5 1. Your task is to use the Scanner methods that will initialize the variables name1, name2, name3, age1, age2, age3 so that the execution of the three println statements below will...

  • Write a Python program that keeps reading in names from the user, until the user enters...

    Write a Python program that keeps reading in names from the user, until the user enters 0. Once the user enters 0, you should print out all the information that was entered by the user. Use this case to test your program: Input: John Marcel Daisy Samantha Nelson Deborah 0 ================ Output: John Marcel Daisy 25 Samantha Nelson Deborah Hints: Create an array Names to hold the input names. Use the Names.append(x) function to add a new name to the...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • Python I'm creating two linked lists from two txt files, merging them, then printing any duplicates...

    Python I'm creating two linked lists from two txt files, merging them, then printing any duplicates found. My program right now only reads numbers one after another following a comma. For example 10,20,15,2. How do i change it so it reads numbers one below another. Like this,                  class Node(object):     value = -1     next = None        def __init__(self,item):         self.val = item         self.next = None def merge_lists(vivendi, activision):     head = tail = Node(0)    ...

  • Write a C++ code to make a simple program that imports the input file in and...

    Write a C++ code to make a simple program that imports the input file in and gets the results as specified Please follow the instructions and paste the code below as the answer with a screenshot of the output to prove it worked. Input file -> https://www.dropbox.com/s/444jofkchu6ylwr/names.txt?dl=0 Scoring Names You will be using the provided names.txt file, text file containing over five-thousand first names, and you will calculate a score for each name. Begin by reading the names from the...

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