1

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.

import pandas as pd

df = pd.read_csv("B.txt", header=None)
B = df. to_numpy()
B=B.tolist()
print("B =",B)

The current output is

B = [['B=3']]

The expected output is

B=[3]
2
  • I think you should skip some lines for reading the file. Commented Sep 21, 2022 at 8:31
  • I skipped some lines but still I am not getting the expected output. I have updated the current output above. Commented Sep 21, 2022 at 8:33

1 Answer 1

1

Add squeeze = True for Series, so ouput is B = ['B=3'], select first value and split, select second value and convert to int:

s  = pd.read_csv("B.txt", header=None, squeeze = True)
print (s)
0    B=3
Name: 0, dtype: object

print (s.iat[0])
B=3
print (s.iat[0].split('='))
['B', '3']

print (s.iat[0].split('=')[1])
3

print("B =", int(s.iat[0].split('=')[1]))
B = 3
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.