[英]Reading a text file using Pandas accurately in Python
我正在嘗試使用pandas
閱讀B.txt
。 它打印B
的值,但不作為列表打印。 我介紹了當前和預期的輸出。
import pandas as pd
df = pd.read_csv("B.txt", header=None)
B = df. to_numpy()
B=B.tolist()
print("B =",B)
當前的 output 是
B = [['B=3']]
預期的 output 是
B=[3]
為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.