I have a DAT file with the following structure:
;Text1
;Text2
;Text3
; N Vec1 Vec2
1 -0.09999 0.09999
2 -0.09999 -0.09999
3 -0.09999 -0.09999
and I want to store the values of Vec1 and Vec2 into two torch tensors.
I tried to use pandas:
import torch
import pandas as pd
df = pd.read_csv('file.DAT',skiprows=4, encoding='latin-1')
vec1 = torch.tensor(df.iloc[:,1].values, dtype=torch.float32)
vec2 = torch.tensor(df.iloc[:,2].values, dtype=torch.float32)
But the problem is I cannot reach the values inside the resulting frame "df". Can someone help? I just want to store Vec1 and Vec2 into two torch tensors.
>Solution :
You can use read_fwf :
df = pd.read_fwf("file.DAT", skiprows=3) #`skip_blank_lines=True` by default
vec1 = torch.tensor(df["Vec1"], dtype=torch.float32)
vec2 = torch.tensor(df["Vec2"], dtype=torch.float32)
Output :
print(vec1) #tensor([-0.1000, -0.1000, -0.1000])
print(vec2) #tensor([ 0.1000, -0.1000, -0.1000])