大佬教程收集整理的这篇文章主要介绍了在 Python 中,从 url 中提取的表中选择子标题的 Panda 语法是什么?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
headers: Volume Energy Enthalpy
Subheaders: vf vg uf ug hf hfg hg
例如:如果我想选择 [Enthalpy,hfg] 我会期待这样的事情:
df = pd.read_HTML('url',header=[2,1])
I used this post as a reference,但这不起作用。
import pandas as pd
def get_data_from_webpage(url):# This returns a List of dataframes
t_table = pd.read_HTML(url,header=1)
return t_table[0]
result = get_data_from_webpage('http://ouopentextbooks.org/thermodynamics/saturation-propertIEs-temperature-table/') # Url with table being passed through function.
print(result)
从这里我想选择临时列用作键,然后将相应行的其余部分放入查找表中。然后当用户输入key时,输出对应的一行值。
最终结果如下:
用户输入: 20
输出: vf、uf、hf 和 sf 值
好的,我做了一个解决方案。但是,您可能必须先执行 pip install Unidecode
才能安装必要的库。
我使用字母作为索引,以免混淆用户,因为列是数字。
输出如下:
List of available temperatures:
AA) 000.01
AB) 005.00
AC) 010.00
AD) 015.00
AE) 020.00
AF) 025.00
AG) 030.00
AH) 035.00
AI) 040.00
AJ) 045.00
AK) 050.00
AL) 055.00
AM) 060.00
AN) 065.00
AO) 070.00
Ap) 075.00
AQ) 080.00
AR) 085.00
AS) 090.00
AT) 095.00
AU) 100.00
AV) 110.00
AW) 120.00
AX) 130.00
AY) 140.00
Az) 150.00
BA) 160.00
BB) 170.00
BC) 180.00
BD) 190.00
BE) 200.00
BF) 210.00
BG) 220.00
BH) 230.00
BI) 240.00
BJ) 250.00
BK) 260.00
BL) 270.00
BM) 280.00
BN) 290.00
BO) 300.00
Bp) 310.00
BQ) 320.00
BR) 330.00
BS) 340.00
BT) 350.00
BU) 360.00
BV) 370.00
BW) 373.95
Enter a temperature (in degrees celsius) from the available options above: 250
vf is: 0.0012519999999999999
uf is: 1080.8
hf is: 1085.8
sf is: 2.7935
代码如下:
import pandas as pd
import os
from unidecode import unidecode
import itertools
import @R_616_10495@ng
import sys
def file_len(fName):
with open(fName) as fp:
for i,line in enumerate(fp):
pass
return i + 1
def excel_cols():
n = 1
while True:
yield from (''.join(group) for group in itertools.product(@R_616_10495@ng.ascii_uppercase,repeat=n))
n += 1
def remove_and_exit(temp13,temp12=0):
try:
os.remove("test.csv")
except:
pass
if temp13:
sys.exit(temp12)
def get_data_from_webpage(url):# This returns a list of dataframes
t_table = pd.read_html(url,header=1)
return t_table[0]
result = get_data_from_webpage('http://ouopentextbooks.org/thermodynamics/saturation-properties-temperature-table/') # Url with table being passed through function.
result.index.rename('index',inplace=TruE)
new_columns = result.columns.values
for temp1 in range(len(new_columns)):
new_columns[temp1] = unidecode(new_columns[temp1])
result.columns = new_columns
remove_and_exit(false)
result.to_csv("test.csv",sep=',',encoding='utf-8')
temp2 = []
with open("test.csv") as file_in:
for line in file_in:
temp2.append(line.@R_616_10495@p())
temp5 = list(itertools.islice(excel_cols(),file_len("test.csv")+26))
temp5 = temp5[26:-1]
temP6 = -1 #before decimal
temP7 = -1 #after decimal
print("List of available temperatures:\n")
for temp3 in range(len(temp2)): #temp5[temp3-1]
if temp3 == 0:
conTinue
if (len(temp2[temp3].split(",")[1].split(".")[0])) > temP6:
temP6 = len(temp2[temp3].split(",")[1].split(".")[0])
if (len(temp2[temp3].split(",")[1].split(".")[1])) > temP7:
temP7 = len(temp2[temp3].split(",")[1].split(".")[1])
for temp8 in range(len(temp2)): #temp5[temp8-1]
if temp8 == 0:
conTinue
print(temp5[temp8-1]+") "+temp2[temp8].split(",")[1].split(".")[0].zfill(temP6)+"."+temp2[temp8].split(",")[1].split(".")[1].ljust(temP7,'0'))
temp4 = input("\nEnter a temperature (in degrees celsius) from the available options above: ")
try:
temp9 = float(temp4)
except:
print("\nYou did not enter a temperature (in degrees celsius)")
remove_and_exit(True,11)
temp11 = false
for temp10 in range(len(temp2)):
if temp10 == 0:
conTinue
if temp9 == float(temp2[temp10].split(",")[1]):
print("")
print("vf is: "+temp2[temp10].split(",")[3])
print("uf is: "+temp2[temp10].split(",")[5])
print("hf is: "+temp2[temp10].split(",")[7])
print("sf is: "+temp2[temp10].split(",")[10])
temp11 = True
break
if not temp11:
print("\nYou did not enter a value from the available options above")
remove_and_exit(True,1761)
remove_and_exit(true)
以上是大佬教程为你收集整理的在 Python 中,从 url 中提取的表中选择子标题的 Panda 语法是什么?全部内容,希望文章能够帮你解决在 Python 中,从 url 中提取的表中选择子标题的 Panda 语法是什么?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。