Skip to content Skip to sidebar Skip to footer

Reading Files As Both Vertical Lists And Horizental Lists(python 3.2)

I have a text file that I want my program (in python 3.2) to read as (1) every line as a list and then (2) I want to make new list that contain elements with same index in (1). lik

Solution 1:

Use .split() to split lines into lists, and use zip(*lines) to turn the rows into columns.

with open('filename') as inputfile:
    rows = [line.split() for line in inputfile]

columns = zip(*rows)

The values in the rows are still string values, but you could now map them to int:

int_columns = [map(int, col) for col in columns[1:]]

This skips the first column, with the names.


Post a Comment for "Reading Files As Both Vertical Lists And Horizental Lists(python 3.2)"