can we create a data frame for the below input:
OP1.T1:OP2.V2
OP1.T1:OP2.V3
OP2.T3:OP2.V2
OP3.T1:OP1.V2
Code tried:
with open("TableView.txt") as f:
for line in f:
line = line.strip()
#print(line)
cols = line.split(':')
my_params = (cols[0],cols[1])
#print(my_params)
df= pd.DataFrame(my_params)
print(df)
i need them likebelow:
BaseS BASET DEPS DEPN
0 OP1 T1 OP2 v2
1 OP1 T1 OP2 v3
2 OP2 T3 OP2 v2
3 OP3 T1 OP1 v2
CodePudding user response:
Use pandas.read_csv
, you can specify \.|:
as separator, which separates on either .
or :
:
from io import StringIO
s = StringIO('''OP1.T1:OP2.V2
OP1.T1:OP2.V3
OP2.T3:OP2.V2
OP3.T1:OP1.V2''')
df = pd.read_csv(s, sep='\.|:', header=None, names=['BaseS', 'BaseT', 'DEPS', 'DEPN'])
df
BaseS BaseT DEPS DEPN
0 OP1 T1 OP2 V2
1 OP1 T1 OP2 V3
2 OP2 T3 OP2 V2
3 OP3 T1 OP1 V2