Python text files utilities

Reading matrix of numbers in text files

Often, you need to manipulate files built like these:

# time current voltage
  0    0       1
  1    0.3     0.9
  2    0.5     0.7

I have a little utility, that let you input and manipulate files like that. For example, you can select and print just the time and voltage values like that:

import sys
import txtmatrix

f1 = txtmatrix.read_datafile(sys.argv[1])
t1 = txtmatrix.subset(f1, ["time", "voltage"])

for i in range(len(t1[0])):
    print t1[0][i], t1[0][i]

read_datafile returns a list of vectors, each vector is one column (time, current, voltage in the example). A vector is simply a list with a “name” attribute. If the input file does not have the first comment line the names are the column positon, starting with 1. The comment line can start with a “#”, a “;” or a “%”, and the starting char can be or not attached to the first name. It can start with just a non-numeric label.

Reading text files from Python
Title: txtmatrix (0 click)
Caption: Reading text files from Python
Size: 1 KB

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>