I am trying to read B.txt
using pandas
. It prints the value of B
but not as a list. I present the current and expected outputs.
JavaScript
x
7
1
import pandas as pd
2
3
df = pd.read_csv("B.txt", header=None)
4
B = df. to_numpy()
5
B=B.tolist()
6
print("B =",B)
7
The current output is
JavaScript
1
2
1
B = [['B=3']]
2
The expected output is
JavaScript
1
2
1
B=[3]
2
Advertisement
Answer
Add squeeze = True
for Series
, so ouput is B = ['B=3']
, select first value and split, select second value and convert to int:
JavaScript
1
16
16
1
s = pd.read_csv("B.txt", header=None, squeeze = True)
2
print (s)
3
0 B=3
4
Name: 0, dtype: object
5
6
print (s.iat[0])
7
B=3
8
print (s.iat[0].split('='))
9
['B', '3']
10
11
print (s.iat[0].split('=')[1])
12
3
13
14
print("B =", int(s.iat[0].split('=')[1]))
15
B = 3
16