Как я могу импортировать несколько столбцов файла в один и тот же массив в Python?Python

Программы на Python
Гость
Как я могу импортировать несколько столбцов файла в один и тот же массив в Python?

Сообщение Гость »


У меня есть файл .txt следующего вида:

Код: Выделить всё

header line 1
header line 2
x1  y1  x4  y4  x7  y7
x2  y2  x5  y5  x8  y8
x3  y3  x6  y6  x9  y9
footer line
the x and y values would be separated by a tab and in my case be numbers of the form "2,9 " (including the last space). Example:

Код: Выделить всё

header line 1
header line 2
1,0     1,5     4,0     4,5     7,0     7,5
2,0     2,5     5,0     5,5     8,0     8,5
3,0     3,5     6,0     6,5     9,0     9,5
footer line
The file is encoded in . I'm looking for an easy way to get numpy arrays of my x and y converted to float, that is:

Код: Выделить всё

array([1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0])
and similarly for y.
I've first created a function to replace "," by "." and remove trailing spaces:

Код: Выделить всё

import numpy as np

def ctf(valstr):
return float(valstr.replace(',','.').replace(" ",""))
then defined a dictionary of variable length to use later:

Код: Выделить всё

def dic(length):
dic={}
for i in range(0,length):
dic[i]=ctf
return dic
I could then "manually" read in the columns and join them together:

Код: Выделить всё

xval1,yval1,xval2,yval2,xval3,yval4=np.genfromtxt("file.txt",delimiter="",unpack=True,skip_header=2,skip_footer=1,encoding="latin-1",converters=dic(6))

xvalues=np.concatenate((xval1,xval2,xval3))
yvalues=np.concatenate((yval1,yval2,yval3))
It works but isn't exactly pretty, especially if I have even more columns. What I would like is a method to only having to specify the total number of columns (in the case above 6) and the number of arrays I want to get (in my example 2).
Note: I don't think the converter/dictionary part is actually relevant for my problem. I included it because I need any alternative solution to be able to use converters or achieve the same result in some other way.


Источник: https://stackoverflow.com/questions/781 ... -in-python

Вернуться в «Python»