Sorting Text File by Using Python

How to sort a text file line-by-line

fn = 'filename.txt'
sorted_fn = 'sorted_filename.txt'

with open(fn,'r') as first_file:
rows = first_file.readlines()
sorted_rows = sorted(rows, key=lambda x: int(x.split()[0]), reverse=False)
with open(sorted_fn,'w') as second_file:
for row in sorted_rows:
second_file.write(row)

This should work for a text file of 3+ million rows. Using int(x.split()[0]) will sort the first item in each row as an integer

Edited to remove close() statements

How can I sort numbers in text file from smallest to largest?

This will print out the 5 smallest numbers

f = open("highscore.txt", "r+")
numbers = sorted(list(map(int, f.readlines())))
print(numbers[:5])

How to sort data of txt file in Python

try this.

def formatheader():
print(
"Code | Name | Producer | Description | Quantity |\n"
"-----+-------------+---------------+------------------+-------------|")

def sortbycode():
device_file = open('devices.txt', 'r')
formatheader()
devices = []
for line in device_file:
devices.append([i for i in line.strip("\n").split(":")])
devices.sort(key=lambda x:x[0])
for device in devices:
print("{0:5}|{1:13}|{2:15}|{3:18}|{4:5}".format(*device))
sortbycode()

Sorting lines of text file numerically when the file has strange formatting

You can do it as follows.

Code

import re

def order_month(month_of_entries):
'''
Order lines for a Month of entries
'''
# Sort key based upon number in line
# First line in Month does not have a number,
# so key function returns 0 for it so it stays first
month_of_entries.sort(key=lambda x: int(p.group(0)) if (p:=re.search('\d+', x)) else 0)

# Process input file
with open('input.txt', 'r') as file:
results = []
months_data = []
for line in file:
line = line.rstrip()
if line:
months_data.append(line)
else:
# blank line
# Order files for this month
order_month(months_data)
results.append(months_data)

# Setup for next month
months_data = []
else:
# Reached end of file
# Order lines for last month
if months_data:
order_entries(months_data)
results.append(months_data)

# Write to output file
with open('output.txt', 'w') as file:
for i, months_data in enumerate(results):
# Looping over each month
for line in months_data:
file.write(line + '\n')
# Add blank line if not last month
if i < len(results) - 1:
file.write('\n')

Output

**January birthdays:**
**4** - !@Jan
**15** - !@Ralph
**17** - !@Mark

**February birthdays:**
**19** - !@Bill
**27** - !@Steve
**29** - !@Bob

Alternativee, that also sort months if necessary

import re
from itertools import accumulate
from datetime import date

def find_day(s, pattern=re.compile(r'\d+')):
return 99 if not s.strip() else int(p.group(0)) if (p:=pattern.search(s)) else 0

def find_month(previous, s, pattern = re.compile(fr"^\*\*({'|'.join(months_of_year)})")):
' Index of Month in year (i.e. 1-12)'
return months_of_year.index(p.group(1)) if (p:=pattern.search(s)) else previous

with open('test.txt') as infile:
lines = infile.readlines()

months_of_year = [date(2021, i, 1).strftime('%B') for i in range(1, 13)] # Months of year
months = list(accumulate(lines, func = find_month, initial = ''))[1:] # Create Month for each line
days = (find_day(line) for line in lines) # Day for each line

# sort lines based upon it's month and day
result = (x[-1] for x in sorted(zip(months, days, lines), key = lambda x: x[:2]))

with open('output.txt', 'w') as outfile:
outfile.writelines(result)

Sort list with information from text file

You most likely have found a solution now. If not, let me suggest something.

First, I made a slight modification to your class: I added a string representation that makes prints more readable:

class A:

def __repr__(self):
return f"A('{self.name}', {self.age})"

def __init__(self, name, age):
self.name = name
self.age = age

You don't need that for the following!

Now the sorting:

def sort_list():
lst = list()
lines = open('my_textfile.txt', 'r').readlines()
for line in lines:
line = line.split()
lst.append(A(line[0], int(line[1])))
lst.sort(key=lambda a: (a.age, a.name))
return lst

Apart from renaming some variables I have made the following adjustments:

  • I converted the second part of the strings to an int because I think that's what you actually want (int(line[1])).
  • After appending all the data to the list it get's sorted. Lists have a sort method, but in your case you have to give it some hints on how to sort, since the elements are a new class. The hint is the lambda function given to the optional key argument of sort. It returns for every element of the list the tuple (a.age, a.name). sort then actually sorts according to this values. Since the age is first, the sorting looks a it first and the result is an increasing age in the final list. For entries with same age the sorting proceeds in alphabetical order on the names.

The result of

lst = sort_list()
print(lst)

is

[A('Kelly', 8), A('Isac', 9), A('Hugo', 10), A('John', 90)]


Related Topics



Leave a reply



Submit