So, I've got this file that's made out of countries and some languages that are being studied there(it's all made up data) and I need to alphabetically sort the file based on 1) the names of the countries and 2) on the languages if there are more than one being studied. I tried converting it to csv and also making a list of lists out of it but nothing seems to work. Help would be appreciated :) Each line is in this format: [Country] [Language] [Number Of Students]
Asked
Active
Viewed 42 times
-2
-
You have added the `python` tag, but you have added no code to your question. – quamrana Jan 11 '21 at 15:55
-
can you provide some sample data and what you would expect the result to be – codingcat3423 Jan 11 '21 at 15:55
-
You can find some useful information here: https://stackoverflow.com/questions/4174941/how-to-sort-a-list-of-lists-by-a-specific-index-of-the-inner-list // https://stackoverflow.com/questions/3368969/find-string-between-two-substrings // https://www.w3schools.com/python/python_file_open.asp – Szala Jan 11 '21 at 16:18
1 Answers
0
Here is a possible solution using Pandas:
import pandas as pd
df = pd.read_csv("file.txt", names=["country", "language", "nstudents"], sep=" ")
df.sort_values(["country", "language"], inplace=True)
df.to_csv("new_file.txt", index=False, header=False, sep=" ")

Riccardo Bucco
- 13,980
- 4
- 22
- 50