Hack Forums
[Python] Sorting in python? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Sorting in python? (/showthread.php?tid=6043018)

Pages: Pages 1 2


Sorting in python? - ThisBearScat - Jan 21, 2020

Hi all,


The bit I'm struggling with is after all actions are completed I then want it to open a .txt file, sort alphabetically and remove duplicates and save it again as a .txt file. It could easily be achieved in a terminal after with a simple command but I wish to automate the process.

Not sure I can post code here so I'm not but I have tried multiple things the closest I got was the results being saved but then the sort() part I added at the end of the script deleted everything and left the .txt document blank.

Anyone know a way I can achieve what is needed?

Thanks for any advice in advance 😃


RE: Sorting in python? - lockhead - Jan 21, 2020

If you just want to remove duplicates you could use a set
https://docs.python.org/2/library/sets.html


RE: Sorting in python? - Charger - Jan 21, 2020

Load the file
Remove duplicates

And you should have a list of each line.
Use the built in “sorted()” function to sort it alphabetically and then write each like back to the file.


RE: Sorting in python? - Lindros - Jan 21, 2020

I actually did a little program like this, here was my code. Sorry it is so broken up, but I was just learning/practicing functions. This was for a CS class awhile back.

Code
def get_user_file_name():
    """
    This function will return the user file
    """
    file_name = input("Enter the input name: ")
    return file_name


def open_user_file(file_name):
    """This function will open the file"""
    my_file = open(f"{file_name}", "r")
    return my_file


def read_file_lines(my_file):
    """This function will read user file to list"""
    my_list = my_file.read()
    return my_list


def remove_list_duplicates(my_list):
    """Function will return an alphabetically sorted list"""
    my_list = list(set(my_list.split()))
    return my_list


def sort_list_alphabetically(my_list):
    """Function will return list sorted alphatetically"""
    return sorted(my_list)


def main():
    file_name = get_user_file_name()
    my_file = open_user_file(file_name)
    my_list = read_file_lines(my_file)
    my_list = remove_list_duplicates(my_list)
    output = sort_list_alphabetically(my_list)
    for word in output:
        print(word)


if __name__ == '__main__':
    main()



RE: Sorting in python? - Riԁdle - Jan 21, 2020

Code
lines = [line.rstrip('\n') for line in open("test.txt")] #puts file contents in a list
lines = set(sorted(lines))  #convert list into a set, removing the dupes, then sort the set
with open("test.txt", "a") as f: #open the file again
    open("test.txt", "w").write("") #open the file a second time & clear it
    for line in lines: # loop through the set
        f.write(line + "\n") #append the set elements to the file



RE: Sorting in python? - ThisBearScat - Jan 22, 2020

I take it I can post code here then lol. Thanks for the suggestion guys, will try them out once I have woke up properly, the script already has a loop on it to process the results and add them to the .txt file line by line.

If you know what XtreamUI is, the script finds the servers and now got it finding the admin panels on the servers and putting the http:// address into a txt file. Next step is the sorting of the txt document and then I have a few more plans for it.

Ps the responses here are better than the coding forums, they was more interested in my grammar than my actual code 😂


(Jan 21, 2020 09:52 PM)Riԁdle Wrote:  
Code
lines = [line.rstrip('\n') for line in open("test.txt")] #puts file contents in a list
lines = set(sort(lines))  #convert list into a set, removing the dupes, then sort the set
with open("test.txt", "a") as f: #open the file again
   open("test.txt", "w").write("") #open the file a second time a clear it
   for line in lines: # loop through the set
       f.write(line + "\n") #append the set elements to the file

hi tried adding this code but it throws an error

Traceback (most recent call last):
File "/media/user/xtreamui-admin.py", line 72, in <module>
lines = set(sort(lines))
NameError: name 'sort' is not defined

any ideas mate?


(Jan 21, 2020 09:08 PM)ZietzSeV Wrote:  I actually did a little program like this, here was my code. Sorry it is so broken up, but I was just learning/practicing functions. This was for a CS class awhile back.

thanks, i tried the code im my script mate and it threw me syntax errors so i opened it on its own and got this

File "/media/user/0CF5-F689/sort.py", line 11
my_file = open(f"{file_name}", "r")
^
SyntaxError: invalid syntax

and this one

Traceback (most recent call last):
File "/media/user/0CF5-F689/sort.py", line 36, in <module>
main()


sorted that and still get syntax errors. i then tried to doctor it to stop the errors but when i eventually got it running it did nothing to the txt file or print anything to terminal so maybe i broke it 😊 😭


RE: Sorting in python? - Lindros - Jan 22, 2020

(Jan 22, 2020 12:34 AM)ThisBearScat Wrote:  I take it I can post code here then lol. Thanks for the suggestion guys, will try them out once I have woke up properly, the script already has a loop on it to process the results and add them to the .txt file line by line.

If you know what XtreamUI is, the script finds the servers and now got it finding the admin panels on the servers and putting the http:// address into a txt file. Next step is the sorting of the txt document and then I have a few more plans for it.

Ps the responses here are better than the coding forums, they was more interested in my grammar than my actual code 😂



hi tried adding this code but it throws anerror

Traceback (most recent call last):
 File "/media/user/xtreamui-admin.py", line 72, in <module>
   lines = set(sort(lines))
NameError: name 'sort' is not defined

any ideas mate?



thanks, i tried the code im my script mate and it threw me syntax errors so i opened it on its own and got this

 File "/media/user/0CF5-F689/sort.py", line 11
   my_file = open(f"{file_name}", "r")
                               ^
SyntaxError: invalid syntax

and this one

Traceback (most recent call last):
 File "/media/user/0CF5-F689/sort.py", line 36, in <module>
   main()


sorted that and still get syntax errors. i then tried to doctor it to stop the errors but when i eventually got it running it did nothing to the txt file or print anything to terminal so maybe i broke it  😊  😭

It worked for me mate, are you using at least Python 3.6? Also I just realized that code actually doesn't write to a new text file but that part is easy. Just reverse engineer the code. It must be messed up from copy-pasting. It pretty simple code tbh.

Are you in a class? If so I recommend you need to go to the tutor lab. Otherwise do you have any Python experience? I can answer specific questions but I see nothing wrong with the code, other than the fact it is only supposed to print to the terminal and not write to a text file (pretty simple).

I don't mean to patronize, everyone has to start somewhere. If you are taking a class, have you guys gotten to functions yet or not?


RE: Sorting in python? - Charger - Jan 22, 2020

(Jan 22, 2020 12:34 AM)ThisBearScat Wrote:  I take it I can post code here then lol. Thanks for the suggestion guys, will try them out once I have woke up properly, the script already has a loop on it to process the results and add them to the .txt file line by line.

If you know what XtreamUI is, the script finds the servers and now got it finding the admin panels on the servers and putting the http:// address into a txt file. Next step is the sorting of the txt document and then I have a few more plans for it.

Ps the responses here are better than the coding forums, they was more interested in my grammar than my actual code 😂



hi tried adding this code but it throws anerror

Traceback (most recent call last):
 File "/media/user/xtreamui-admin.py", line 72, in <module>
   lines = set(sort(lines))
NameError: name 'sort' is not defined

any ideas mate?



thanks, i tried the code im my script mate and it threw me syntax errors so i opened it on its own and got this

 File "/media/user/0CF5-F689/sort.py", line 11
   my_file = open(f"{file_name}", "r")
                               ^
SyntaxError: invalid syntax

and this one

Traceback (most recent call last):
 File "/media/user/0CF5-F689/sort.py", line 36, in <module>
   main()


sorted that and still get syntax errors. i then tried to doctor it to stop the errors but when i eventually got it running it did nothing to the txt file or print anything to terminal so maybe i broke it  😊  😭

open(f"{file_name}", "r")

Should be

open(“{file_name}", "r")


RE: Sorting in python? - Riԁdle - Jan 23, 2020

(Jan 22, 2020 12:34 AM)ThisBearScat Wrote:  -snip-

sorry for that, I edited the file before I posted it and I invoked sort but it wasn't a function, it's sorted.. I fixed now & it will work


RE: Sorting in python? - vorticalbox - Jan 23, 2020

(Jan 22, 2020 08:52 PM)Charger Wrote:  open(f"{file_name}", "r")

Should be

open(“{file_name}", "r")


no it shouldn't the f does a string replace to change {file_name} to the name that was input. now you're literally opening a file called {file_name}


RE: Sorting in python? - Goodies - Jan 23, 2020

Jesus Christ you guys are over-complicating this (Click to View)

1) Open the file
2) Create a set literal containing the stripped lines of the file (automatically removes duplicates)
3) Use `sorted` builtin for sorting. Default will sort strings alphabetically.

PHP Code

<?php 
def read_sort_deduplicate
(filename):
 
   with open(filename, "r") as f:
 
       return sorted({line.strip() for line in f}) 

Example File:
Code
c
b
a
c
d
a

Extremely simple usage:
PHP Code

<?php 
>>> def read_sort_deduplicate(filename):
... 
    with open(filename, "r") as f:
... 
        return sorted({line.strip() for line in f})
...
>>> 
read_sort_deduplicate("test.txt")
[
'a', 'b', 'c', 'd'] 



RE: Sorting in python? - ThisBearScat - Jan 23, 2020

(Jan 22, 2020 11:46 AM)ZietzSeV Wrote:  It worked for me mate, are you using at least Python 3.6? Also I just realized that code actually doesn't write to a new text file but that part is easy. Just reverse engineer the code. It must be messed up from copy-pasting. It pretty simple code tbh.

Are you in a class? If so I recommend you need to go to the tutor lab. Otherwise do you have any Python experience? I can answer specific questions but I see nothing wrong with the code, other than the fact it is only supposed to print to the terminal and not write to a text file (pretty simple).

I don't mean to patronize, everyone has to start somewhere. If you are taking a class, have you guys gotten to functions yet or not?

I'm currently using parrot os, writing in pluma and my python version is Python 2.7.16+, maybe that is the issue, I'm not in no class mate, im a middle age man with a rare ilness that just sits at home all day trying to pass my time. My main area is OSINT and PI and im not brilliant with python but i know about functions and have played around with python for years but never really created anything so i started this project so i can further my knowledge and learn along the way.

(Jan 23, 2020 12:42 AM)Riԁdle Wrote:  sorry for that, I edited the file before I posted it and I invoked sort but it wasn't a function, it's sorted.. I fixed now & it will work

No worries il give it another spin and see what happens, thanks again :)

(Jan 23, 2020 01:49 AM)Goodies Wrote:  Jesus Christ you guys are over-complicating this (Click to View)

1) Open the file
2) Create a set literal containing the stripped lines of the file (automatically removes duplicates)
3) Use `sorted` builtin for sorting. Default will sort strings alphabetically.

PHP Code

<?php 
def read_sort_deduplicate
(filename):
 
   with open(filename, "r") as f:
 
       return sorted({line.strip() for line in f}) 

Example File:
Code
c
b
a
c
d
a

Extremely simple usage:
PHP Code

<?php 
>>> def read_sort_deduplicate(filename):
... 
    with open(filename, "r") as f:
... 
        return sorted({line.strip() for line in f})
...
>>> 
read_sort_deduplicate("test.txt")
[
'a', 'b', 'c', 'd'] 

Tthanks i will have a good look at all sugestions again and think i might update my python incase that is the issue.

Thanks all for your advice and helping me on my journey :) im sure between the answers i will acheive it. when i do il come back and post the code that worked to help others on the same journey as me ✌


(Jan 21, 2020 09:52 PM)Riԁdle Wrote:  
Code
lines = [line.rstrip('\n') for line in open("test.txt")] #puts file contents in a list
lines = set(sorted(lines))  #convert list into a set, removing the dupes, then sort the set
with open("test.txt", "a") as f: #open the file again
   open("test.txt", "w").write("") #open the file a second time a clear it
   for line in lines: # loop through the set
       f.write(line + "\n") #append the set elements to the file

Riddle your a STAR mate 👍 🤩 works like a charm, i had a feeling you was close yesterday other than the error because i had to use .write(""+URL+"" "\n") for it to store the results in the first place. i must admit i did have problems with that part too as it was storing all the data on one line but google searches helped me lol thats what made me think you had it right because the way your code read line by line, sorted and wrote back to same file line by line.

Really happy thanks everyone for your time and input, i will still have a look at the other examples given as its a good chance for me to learn more and see different examples of how to acheive sorting. also going to examine both riddle's examples from yesterday and today so i can work out why it didnt work yesterday and how it was solved.

✌


RE: Sorting in python? - Lindros - Jan 23, 2020

Yeah my program is python 3


RE: Sorting in python? - Flash7475 - Jan 23, 2020

I might be a little late to the game since the solutions have already been posted but here is my implementation. I find this a more straight forward approach since the code might be a little more easier to read.

PHP Code

<?php 
import sys
import os


def main
():
 
   """Sort a given text file alphabetically"""

 
   #open unsorted file and open a new file called sorted
 
   file_in = open("names.txt", 'r')
 
   file_out = open("names_sorted.txt", 'w')

 
   #Create new list and append everything in unsorted file to a list, stripping the new line char
 
   master_list = []
 
   for line in file_in:
 
       master_list.append(line.rstrip())
 
   
    
#sort the list
 
   sorted_master_list = sorted(master_list)
 
   
    
#print all newly sorted items into a sorted text file
 
   for item in sorted_master_list:
 
       file_out.write(item)
 
       file_out.write("\n")

 
   #Close both files
 
   file_out.close()
 
   file_in.close()

if 
__name__ == "__main__":
 
   main() 



RE: Sorting in python? - ThisBearScat - Jan 25, 2020

Thanks for the input mate, it's good for me to see and study all the different methods of acheiving the same outcome. 😃