Hi! I am trying to make a program that will put the bands I've played with in alphabetical order. I'm getting the band names from a file on my computer. I am fairly new to python and don't have much experience what so ever. I'll post the code that I have so far. I feel like going in the wrong direction. If anyone could help me out that would be great.

document = file("bands.txt")
alphabet = 'abcdefghijklmnopqrstuvwxyz'

for line in document:
    print line
    for alphabet in line.split():
        alphabet = alphabet.lower()
        alphabet = alphabet.strip(".,?!:;\"'[]{}\|@#$%^+=*-+?")
        if len(alphabet) < 0:
            continue
        print alphabet
        if alphabet not in alphabet:
            alphabet[alphabet] = 0
        alphabet [alphabet] =+ 1

The program prints out the first band in the document but gives me an error after that.

Thanks so much for the help.

Recommended Answers

All 3 Replies

Looks quite nice but line 2 (overwriten by for) and last three lines does not make sense to me. i have written code snippet for producing lowercased words from file, check it out from code snippet archive

Thanks! Yeah I just did some work on the the code. It's now printing out all of the bands just not in alphabetical order. Do I need to do it in ascending order? Any feedback with help. Thanks so much!

Here is my new code.

document = file("bands.txt")
alphabet = 'abcdefghijklmnopqrstuvwxyz'

for line in document:
    print line
    for alphabet in line.split():
        alphabet = alphabet.lower()
        alphabet = alphabet.strip(".,?!:;\"'[]{}\|@#$%^+=*-+?")
        if len(alphabet) <1:
            continue
        print alphabet

Thanks! Yeah I just did some work on the the code. It's now printing out all of the bands just not in alphabetical order. Do I need to do it in ascending order? Any feedback with help. Thanks so much!

Here is my new code.

Let's condense it little and see if we can produce generator for sorted() function:

bands = sorted(band for band in set(word.lower().strip(".,?!:;\"'[]{}\|@#$%^+=*-+?") # set removes duplicates
                                   for line in open("bands.txt") # open is more regular way to open file
                                   for word in line.split())
               if band) # filter empty words

I do not have possibility testing it as I have not sample of your data.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.