1

I've looked around for some tutorials and still can't come up with any way to sort this file due to it's really weird sorting!

The file goes as: (Where x is a number, y is a date, z is time and b is a number)

x x x x x
y y y y y
z z z z z
b b b b b

There's actually 50 entries in each row but that's an extremely simplified version of the file.

What I'm struggling to do is to sort each entry so that it's [x,y,z,b]

All I've come up with so far is to split each row into a list, but that's clearly not what I need to do and thus I'm stuck.

import csv

with open('sample.txt') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=' ')
    for row in readCSV:
        print(row)
2
  • 4
    I think I'm confused by your use of the word "sort". It sounds like what you actually want to do is pivot it, so that rows become columns and vice versa, is that right? Commented Jan 20, 2017 at 12:28
  • @DanielRoseman Yes! Exactly. Sorry for being confusing, how exactly would you pivot that file? Commented Jan 20, 2017 at 12:48

4 Answers 4

2

Numpy's tranpose is what you look for, i.e.

from csv import reader
from numpy import transpose
file = open("file.dat")
results = reader(file, delimiter=' ')

print(transpose(list(results)))
Sign up to request clarification or add additional context in comments.

2 Comments

This results in the exact same result as the code above, what I want to do is to separate it.
Separate each column? Do res = transpose(list(results)) and call res[i] for i+1th column of your initial file.
0

sort file:

x1  x2  x3  x4  x5
y   y   y   y   y
z   z   z   z   z
b   b   b   b   b

Program:

import csv

with open("C:\\Users\\sortfile.csv", 'r') as fopen:
    rows = csv.reader(fopen)
    csvf = list(rows)

x = 0
array = []

while True:
    lines = []
    try:
        for i in csvf:
            lines.append(i[x])
        array.append(lines)
        x +=1
    except IndexError:
        break

Results:

[['x1', 'y', 'z', 'b'], ['x2', 'y', 'z', 'b'], ['x3', 'y', 'z', 'b'], ['x4', 'y', 'z', 'b'], ['x5', 'y', 'z', 'b']]

I hope I understood your question correctly.

Comments

0

This opens whatever test.txt file you have with the original values, goes through line by line and constructs a transposed array of arrays. Then it iterates through these and prints them into newText.txt in the order you've asked for as well as a csv. Not sure which you want more :) .

import csv

newLine = []
newArr = []

x = 0

with open ('text.txt', 'r') as f:
    end = len(f.readline().replace(' ',''))
    f.seek(0)
    while x <= end:
        for line in f:
        newLine.append(line.split(" ")[x])
        newArr.append(newLine)
    x += 1

# outputs a text file
with open ('newText.txt', 'wb') as newf:
    for line in newArr:
        newf.write(' '.join(line) + '\n')

# outputs a csv file
with open('text.csv', 'wb') as csvfile:
    writeCSV = csv.writer(csvfile)
    for line in newArr:
        writeCSV.writerow(''.join(line))

Comments

0

Given:

$ cat so.csv
x1,x2,x3,x4,x5
y1,y2,y3,y4,y5
z1,z2,z3,z4,z5
b1,b2,b3,b4,b5

You would normally read the file line by line like so:

import csv

LoL=[]
with open(fn) as f:
    for row in csv.reader(f):
        LoL.append(row)

Or, more compactly:

LoT=[row for row in csv.reader(f)]

>>> LoL
[['x1', 'x2', 'x3', 'x4', 'x5'], ['y1', 'y2', 'y3', 'y4', 'y5'], ['z1', 'z2', 'z3', 'z4', 'z5'], ['b1', 'b2', 'b3', 'b4', 'b5']]

You can transpose that with zip:

with open(fn) as f:
    LoT=[row for row in zip(*csv.reader(f))]

>>> LoT
[('x1', 'y1', 'z1', 'b1'), ('x2', 'y2', 'z2', 'b2'), ('x3', 'y3', 'z3', 'b3'), ('x4', 'y4', 'z4', 'b4'), ('x5', 'y5', 'z5', 'b5')]

Then you can write transposed list of tuples to a new csv or process as you wish.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.