简体   繁体   English

在 Python 中准确读取使用 Pandas 的文本文件

[英]Reading a text file using Pandas accurately in Python

I am trying to read B.txt using pandas .我正在尝试使用pandas阅读B.txt It prints the value of B but not as a list.它打印B的值,但不作为列表打印。 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当前的 output 是

B = [['B=3']]

The expected output is预期的 output 是

B=[3]

Add squeeze = True for Series , so ouput is B = ['B=3'] , select first value and split, select second value and convert to int:Series添加squeeze = True ,因此输出为B = ['B=3'] , select 第一个值并拆分, select 第二个值并转换为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM