2

Goal: Sort the text file alphabetically based on the characters that appear AFTER the final slash. Note that there are random numbers right before the final slash.

Contents of the text file:

https://www.website.com/1939332/delta.html
https://www.website.com/2237243/alpha.html
https://www.website.com/1242174/zeta.html
https://www.website.com/1839352/charlie.html

Desired output:

https://www.website.com/2237243/alpha.html
https://www.website.com/1839352/charlie.html
https://www.website.com/1939332/delta.html
https://www.website.com/1242174/zeta.html

Code Attempt:

i = 0
for line in open("test.txt").readlines(): #reading text file
    List = line.rsplit('/', 1)            #splits by final slash and gives me 4 lists
    dct = {list[i]:list[i+1]}             #tried to use a dictionary 
    sorted_dict=sorted(dct.items())       #sort the dictionary

textfile = open("test.txt", "w")
for element in sorted_dict:
    textfile.write(element + "\n")
textfile.close()
   

Code does not work.

  • 1
    What does "does not work" mean? What happens when you run the code? What do you want it to do instead? Have you tried debugging your code? Check out [this article](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) for some tips. – Code-Apprentice May 09 '22 at 21:10

3 Answers3

3

I would pass a different key function to the sorted function. For example:

with open('test.txt', 'r') as f:
    lines = f.readlines()
    lines = sorted(lines, key=lambda line: line.split('/')[-1])

with open('test.txt', 'w') as f:
    f.writelines(lines)

See here for a more detailed explanation of key functions.

0x5453
  • 12,753
  • 1
  • 32
  • 61
1

Before you run this, I am assuming you have a newline at the end of your test.txt. This will fix "combining the second and third lines".

If you really want to use a dictionary:

dct = {}
i=0
with open("test.txt") as textfile:
    for line in textfile.readlines(): 
        mylist = line.rsplit('/',1)
        dct[mylist[i]] = mylist[i+1]

sorted_dict=sorted(dct.items(), key=lambda item: item[1])

with open("test.txt", "w") as textfile:
    for element in sorted_dict:
        textfile.write(element[i] + '/' +element[i+1])

What you did wrong

In the first line, you name your variable List, and in the second you access it using list.

List = line.rsplit('/', 1)
dct = {list[i]:list[i+1]}

Variable names are case sensitive so you need use the same capitalisation each time. Furthermore, Python already has a built-in list class. It can be overridden, but I would not recommend naming your variables list, dict, etc.

( list[i] will actually just generate a types.GenericAlias object, which is a type hint, something completely different from a list, and not what you want at all.)

You also wrote

dct = {list[i]:list[i+1]}

which repeatedly creates a new dictionary in each loop iteration, overwriting whatever was stored in dct previously. You should instead create an empty dictionary before the loop, and assign values to its keys every time you want to update it, as I have done.

You're calling sort in each iteration in the loop; you should only call once it after the loop is done. After all, you only want to sort your dictionary once.

You also open the file twice, and although you close it at the end, I would suggest using a context manager and the with statement as I have done, so that file closing is automatically handled.


My code

sorted(dct.items(), key=lambda item: item[1])

means that the sorted() function uses the second element in the item tuple (the dictionary item) as the 'metric' by which to sort.

`textfile.write(element[i] + '/' +element[i+1])` 

is necessary, since, when you did rsplit('/',1), you removed the /s in your data; you need to add them back and reconstruct the string from the element tuple before you write it.

You don't need + \n in textfile.write since readlines() preserves the \n. That's why you should end text files with a newline: so that you don't have to treat the last line differently.

thariqfahry
  • 409
  • 3
  • 8
0
def sortFiles(item):
    return item.split("/")[-1]

FILENAME = "test.txt"

contents = [line for line in open(FILENAME, "r").readlines() if line.strip()]

contents.sort(key=sortFiles)

with open(FILENAME, "w") as outfile:
    outfile.writelines(contents)
Leshawn Rice
  • 617
  • 4
  • 13