• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Python code to read in data file, write it to a file, order columns in ascending order


New Coder
Specific Prompt
Read in the included data file and write it to a file named sorted_data.txt with the same data, but order the columns in ascending alphabetical order.

The data comes from a .txt file and is randomly generated data going up to 1000

How would I approach writing this code?

Attachment contains fake data from https://mockaroo.com/


  • MOCK_DATA-3 (3).txt
    111 KB · Views: 3
Last edited by a moderator:


King Coder
It appears that each column value is separated by a tabbed space. I didn't check each one though.
If this is the case, you can do a split() to separate by new lines. Then in a loop, use split to separate by tabbed space. If it's the first line, then these split values are the column names and should be stored in a column list. If after first line, it is a data row. You can use sort() to auto-sort the column list alphabetically. Now, obviously the columns are in a different order. I can look later, but you should be able to Google how to sort a list based on another list's changes