Home > front end >  sort row's similar columns and put row other items below the sorted row name
sort row's similar columns and put row other items below the sorted row name

Time:05-22

Sort rows names and print the corresponding values below them.

text file contains

x 1 asd
x 2 asd
x 3 asd
x 4 asd
x 5 asd
x 5 asd
x 7 asd
b 8 axy
b 9 axc

output required

x 
asd
asd
asd
asd
asd
asd
asd

b
axy
axc

CodePudding user response:

Use csv reader

import csv

with open("file.txt", "r") as f:
    for row in csv.reader(f, delimiter=' '):
        if first:
            print(row[0])
            first = False
        print(row[-1])

CodePudding user response:

Here's a way to do it:

with open('infile.txt', 'r', encoding="utf-8") as f:
    rows = [row.split() for row in f.readlines()]
    print('rows:'); print(rows)
    column = [rows[0][0]]   [row[-1] for row in rows]
    print('column:'); print(column)
    with open('outfile.txt', 'w', encoding="utf-8") as g:
        for row in column:
            g.write(f'{row}\n')

# check the output file:
with open('outfile.txt', 'r', encoding="utf-8") as f:
    print('contents of output file:')
    [print(row.strip('\n')) for row in f.readlines()]

Explanation:

  • read all lines from the input file and split each into a list of tokens, creating a list of lists named rows
  • create a list named column whose first element is the top left element of rows, with the remaining elements coming from the right column of rows
  • write the contents of column to the output file one at a time, each on its own line using \n as the line terminator
  • read and print the output file to check it contains the desired output (taking care to to strip the final \n since print() appends its own \n)

Output:

rows:
[['x', '1', 'asd'], ['x', '2', 'asd'], ['x', '3', 'asd'], ['x', '4', 'asd'], ['x', '5', 'asd'], ['x', '5', 'asd'], ['x', '7', 'asd']]
column:
['x', 'asd', 'asd', 'asd', 'asd', 'asd', 'asd', 'asd']
contents of output file:
x
asd
asd
asd
asd
asd
asd
asd

UPDATE: Addressing OP's modified question.

with open('infile.txt', 'r', encoding="utf-8") as f:
    rows = [row.split() for row in f.readlines()]
    print('rows:'); print(rows)
    columns = []
    col = []
    iRow = 0
    while iRow < len(rows):
        if iRow == 0 or rows[iRow - 1][0] != rows[iRow][0]:
            if iRow > 0:
                columns.append(col)
            col = [rows[iRow][0]]
        col.append(rows[iRow][-1])
        iRow  = 1
    columns.append(col)
    print('columns:'); print(columns)
    
    #column = [rows[0][0]]   [row[-1] for row in rows]
    #print('column:'); print(column)
    with open('outfile.txt', 'w', encoding="utf-8") as g:
        isFirstCol = True
        for column in columns:
            if isFirstCol:
                isFirstCol = False
            else:
                g.write(f'\n')
            for row in column:
                g.write(f'{row}\n')

# check the output file:
with open('outfile.txt', 'r', encoding="utf-8") as f:
    print('contents of output file:')
    [print(row.strip('\n')) for row in f.readlines()]

Output:

rows:
[['x', '1', 'asd'], ['x', '2', 'asd'], ['x', '3', 'asd'], ['x', '4', 'asd'], ['x', '5', 'asd'], ['x', '5', 'asd'], ['x', '7', 'asd'], ['b', '8', 'axy'], ['b', '9', 'axc']]
columns:
[['x', 'asd', 'asd', 'asd', 'asd', 'asd', 'asd', 'asd'], ['b', 'axy', 'axc']]
contents of output file:
x
asd
asd
asd
asd
asd
asd
asd

b
axy
axc
  • Related