1

Say, I have an Excel file exported as a CSV file, 5 rows and 3 columns, with the following values:

1.0 0.0 5.0
2.0 0.0 4.0
3.0 0.0 3.0
4.0 0.0 2.0
5.0 0.0 1.0

I need to get a list of lists with the sorted values of the correlative columns (in this example 3 columns, but it could be more...), like:

OutputList = [[1.0, 2.0, 3.0, 4.0, 5.0], [0.0, 0.0, 0.0, 0.0, 0.0], [5.0, 4.0, 3.0, 2.0, 1.0]]

Unfortunately I cannot use Pandas. All answers I found were related to pandas or listing values in rows instead of columns (or code snippets that didn't work for me).

5
  • Then remove tag pandas Commented Apr 23, 2018 at 16:34
  • What about using csv reader? Have you looked at this possible duplicate question? Commented Apr 23, 2018 at 16:36
  • OK, thanks for the tip, @Wen Commented Apr 23, 2018 at 16:36
  • Does your file have a header? Commented Apr 23, 2018 at 16:37
  • No, it doesn't, @pault. That's why I posted the desired 'OutputList' including values in first row. Commented Apr 23, 2018 at 16:38

4 Answers 4

2

Using default csv module

Demo:

import csv
with open(filename, "r") as infile:
    reader = csv.reader(infile, delimiter=' ')
    OutputList = [map(float, list(i)) for i in zip(*reader)]

print(OutputList)

Output:

[[1.0, 2.0, 3.0, 4.0, 5.0], [0.0, 0.0, 0.0, 0.0, 0.0], [5.0, 4.0, 3.0, 2.0, 1.0]]

Edit as per comment.

from itertools import izip_longest
import csv
with open(filename, "r") as infile:
    reader = csv.reader(infile, delimiter=' ')
    OutputList = [map(float, [j for j in list(i) if j is not None]) for i in izip_longest(*reader)]

print(OutputList)
Sign up to request clarification or add additional context in comments.

7 Comments

In python3 you need to use list(map(float, list(i)))
python3 OutputList = [list(map(float, list(i))) for i in zip(*reader)]
In Python 3, you should open csv files and specify newline='' according to the documentation (see example code).
After trying a few times... finally i got it! with delimiter=';' It was the way the csv was actually stored. Thank you very much!
@martineau. Thanks, sorry I only have python2.7 in my machine.
|
2

You could try it with the defaul csv module and the zip function:

import csv
with open('book1.csv') as f:
    reader = csv.reader(f)
    a = list(zip(*reader))
    for i in a:
        print(i)

Output is:

('1.0', '2.0', '3.0', '4.0', '5.0')
('0.0', '0.0', '0.0', '0.0', '0.0')
('5.0', '4.0', '3.0', '2.0', '1.0')

Comments

2

Here is one approach to your problem without using pandas or csv:

Read the file into a list of rows and then use zip to convert it into a list of columns:

delim = ";"  # based on OP's comment
with open("myfile") as f:
    OutputList = [[float(x) for x in line.split(delim)] for line in f]
OutputList = zip(*OutputList)

print(OutputList)
#[(1.0, 2.0, 3.0, 4.0, 5.0),
# (0.0, 0.0, 0.0, 0.0, 0.0),
# (5.0, 4.0, 3.0, 2.0, 1.0)]

This produces a list of tuples. If you wanted to change those to lists, you can easily convert them using:

OutputList = [list(val) for val in OutputList]
print(OutputList)
#[[1.0, 2.0, 3.0, 4.0, 5.0],
# [0.0, 0.0, 0.0, 0.0, 0.0],
# [5.0, 4.0, 3.0, 2.0, 1.0]]

2 Comments

Thank you very much, mate. For some reason I just got ' WARNING: Script error: "invalid literal for float(): 1;2;5" at line number 4' and I think it's related with the specification of the delimiter. Thank you very much anyway, mate!!
@Victor str.split() takes an optional delimiter argument (default is whitespace). I added an update to fix this for your code.
1
def sort_columns(myfile):
    # open the file with your data
    with open(myfile, "r") as f:
        # read the data into a "rows"
        rows = f.readlines()

    # store the number of columns or width of your file
    width = len(rows[0].split())
    # initialize your "result" variable that will be a list of lists
    result = []
    # initialize i to 0 and use it access each column value from your csv data
    i = 0
    while i < width:
        # initialize temp list before each while loop run
        temp = []
        # using list comprehension, store the i'th column from each row into temp
        temp = [ float(row.split()[i]) for row in rows if row.split() ]
        # temp now has the value of entire i'th column, append this to result
        result.append(temp)
        # increment i to access the next column
        i += 1
    # return your result
    return result

print sort_columns("file-sort-columns.txt")

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.