[英]How to extract specific text and some extra characters from a string in python?
[英]How To Remove Some Characters from a String in Python or just Extract some lines from it?
我正在使用 Pandas 接收實時黃金價格,我只想清理輸出,使其看起來不錯且可讀。 請幫我解決這個問題。
我的代碼:
import pandas as pd
d = pd.read_html('http://www.livepriceofgold.com/pakistan-gold-price.html')
type(d)
a=len(d)
i=1
df = d[3]
finalString=df.to_string()
print(finalString)
和輸出是:
0 1 2
0 NaN Gold Rate in PKR Pakistani rupee Rate
1 NaN Gold Rate per Gram in PKR 7 889.65
2 NaN Gold Rate per Oz in PKR 245 368.02
3 NaN Gold Rate per KG in PKR 7 889 646.96
4 NaN Gold Rate per Tola in PKR 92 023.26
我想要這種類型的輸出:
Gram in PKR: 7 889.65
Oz in PKR : 245 368.02
KG in PKR : 7 889 646.96
Tola in PKR: 92 023.26
或者只是提取變量中的費率
您可以將第一行數據的header=0
參數添加到列名稱中, thousands=' '
用於在read_html
中刪除read_html
中的空格。
然后通過使用DataFrame.iloc
索引刪除第一列,設置新列名稱並通過Series.replace
更改Text
列中的Series.replace
:
url = 'http://www.livepriceofgold.com/pakistan-gold-price.html'
df = pd.read_html(url, header=0, thousands=' ')[3].iloc[:, 1:]
df.columns= ['Text','Rates']
df['Text'] = df['Text'].replace('Gold Rate per ', '', regex=True)
print (df)
Text Rates
0 Gram in PKR 7889.65
1 Oz in PKR 245368.02
2 KG in PKR 7889646.96
3 Tola in PKR 92023.26
到目前為止,我已經使用它來刪除所有內容,然后拆分這些字符串並使用 split 函數提取我需要的值
fs=fs.replace("Gold Rate per Gram in PKR","")
fs=fs.replace("Gold Rate per Oz in PKR","")
fs=fs.replace("Gold Rate per KG in PKR","")
fs=fs.replace("Gold Rate per Tola in PKR","")
fs=fs.replace("Gold Rate in PKR Pakistani rupee","")
fs=fs.replace("Rate","")
fs=fs.replace("0 NaN","")
fs=fs.replace("1 NaN","")
fs=fs.replace("2 NaN","")
fs=fs.replace("3 NaN","")
fs=fs.replace("4 NaN","")
#print(fs.split())
single= fs.split()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.