我编写了一个函数,它在输入一个列表后生成一个表。
这是我正在做的网页抓取脚本的一部分。
功能工作(不是最好的,但足以达到其目的),但有没有更好的方法来实现更好/类似/相同的结果?
例如,下面是一个列表,我希望将其转换为表格:
listings =
["Search Result", "Advanced Search", "Item Trader Location Price Last Seen", "Sealed Blacksmithing Writ", "Rewards 356 Vouchers",
"Level 1", "@rscus2001", "Shadowfen: Stormhold", "Ghost Sea Trading Co", "71,200", "X", "1", "=", "71,200 3 Hour ago", "Sealed Blacksmithing Writ", "Rewards 328 Vouchers",
"Level 1", "@Deirdre531", "Grahtwood: Elden Root", "piston", "100,000", "X", "1", "=", "100,000 6 Hour ago", "Sealed Blacksmithing Writ", "Rewards 328 Vouchers",
"Level 1", "@Araxas", "Luminous Legion", "100,000", "X", "1", "=", "100,000 9 Hour ago", "Sealed Blacksmithing Writ", "Rewards 356 Vouchers",
"Level 1", "@CaffeinatedMayhem", "Craglorn: Belkarth", "Masser's Merchants", "25,000", "X", "1", "=", "25,000 13 Hour ago", "Sealed Blacksmithing Writ", "Rewards 287 Vouchers",
"Level 1", "@Gregori_Weissteufel", "Wrothgar: Morkul Stronghold", "The Cutthroat Mutineers", "45,000", "X", "1", "=", "45,000 13 Hour ago", "<", "1", ">"]
结果:
0 1 2 3 4 5 6 7 8 9 10
0 Sealed Blacksmithing Writ Rewards 356 Vouchers Level 1 @rscus2001 Shadowfen: Stormhold Ghost Sea Trading Co 71,200 X 1 = 71,200 3 Hour ago
1 Sealed Blacksmithing Writ Rewards 328 Vouchers Level 1 @Deirdre531 Grahtwood: Elden Root piston 100,000 X 1 = 100,000 6 Hour ago
2 Sealed Blacksmithing Writ Rewards 328 Vouchers Level 1 @Araxas Luminous Legion 100,000 X 1 = 100,000 9 Hour ago None
3 Sealed Blacksmithing Writ Rewards 356 Vouchers Level 1 @CaffeinatedMayhem Craglorn: Belkarth Masser's Merchants 25,000 X 1 = 25,000 13 Hour ago
4 Sealed Blacksmithing Writ Rewards 287 Vouchers Level 1 @Gregori_Weissteufel Wrothgar: Morkul Stronghold The Cutthroat Mutineers 45,000 X 1 = 45,000 13 Hour ago
下面是我的代码:
import re
import pandas as pd
pd.set_option('display.max_columns', None)
pd.options.display.width=None
def MakeTable(listings):
hour_idx = [i for i, item in enumerate(listings) if re.search(r"([0-9,]*\s[0-9]*\s(Minute|Hour)\sago|[0-9,]*\sNow)", item)]
if len(hour_idx) == 1:
ls = [listings[3:hour_idx[0]+1]]
elif len(hour_idx) == 2:
ls = [listings[3:hour_idx[0]+1],listings[hour_idx[0]+1:hour_idx[1]+1]]
elif len(hour_idx) == 3:
ls = [listings[3:hour_idx[0]+1],listings[hour_idx[0]+1:hour_idx[1]+1],listings[hour_idx[1]+1:hour_idx[2]+1]]
elif len(hour_idx) == 4:
ls = [listings[3:hour_idx[0]+1],listings[hour_idx[0]+1:hour_idx[1]+1],listings[hour_idx[1]+1:hour_idx[2]+1],listings[hour_idx[2]+1:hour_idx[3]+1]]
else:
ls = [listings[3:hour_idx[0]+1],listings[hour_idx[0]+1:hour_idx[1]+1],listings[hour_idx[1]+1:hour_idx[2]+1],listings[hour_idx[2]+1:hour_idx[3]+1],listings[hour_idx[3]+1:hour_idx[4]+1]]
df = pd.DataFrame(ls)
print(df)
3条答案
按热度按时间bttbmeg01#
我们可以用
list
和comprehensions
来陈述:wrrgggsh2#
我想已经有人接了--但我还是去了一小会儿:
据我所见,它和你发布的版本做的完全一样。
s4n0splo3#
Python 3.10中,你可以编写如下语法的switch语句: