У меня есть файл .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
Код: Выделить всё
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
Код: Выделить всё
latin-1Код: Выделить всё
array([1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0])
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(" ",""))
Код: Выделить всё
def dic(length):
dic={}
for i in range(0,length):
dic[i]=ctf
return dic
Код: Выделить всё
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))
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