gpt4 book ai didi

python - 从 CSV 输出制作 DataFrame

转载 作者:太空宇宙 更新时间:2023-11-03 15:32:35 26 4
gpt4 key购买 nike

这是我的代码:

import requests, re, pandas, csv
from bs4 import BeautifulSoup

r=requests.get("http://www.hltv.org/?pageid=188&statsfilter=2816&offset=0")
c=r.content

table=BeautifulSoup(c,"html.parser")

for row in table.find_all('div', style=re.compile(r'width:606px;height:22px;background-color')):
data=row.get_text(strip=True, separator=',')
print(data)

这是抓取的输出:

5/3 17,Astralis (16),FaZe (13),inferno,IEM Katowice 2017
5/3 17,Astralis (16),FaZe (12),nuke,IEM Katowice 2017
5/3 17,Astralis (16),FaZe (12),overpass,IEM Katowice 2017
5/3 17,FaZe (16),Astralis (9),cache,IEM Katowice 2017
4/3 17,Astralis (16),Heroic (12),nuke,IEM Katowice 2017
4/3 17,Astralis (16),Heroic (12),train,IEM Katowice 2017
4/3 17,Immortals (10),FaZe (16),mirage,IEM Katowice 2017
2/3 17,Virtus.pro (14),Heroic (16),nuke,IEM Katowice 2017
2/3 17,Cloud9 (6),Natus Vincere (16),mirage,IEM Katowice 2017
2/3 17,SK (16),North (8),cbble,IEM Katowice 2017
2/3 17,Cloud9 (12),North (16),cbble,IEM Katowice 2017
2/3 17,Natus Vincere (12),Heroic (16),overpass,IEM Katowice 2017
2/3 17,Virtus.pro (16),SK (14),inferno,IEM Katowice 2017

从此输出生成 pandas.DataFrame 的好方法是什么?

最佳答案

您可以使用函数pandas.read_csv 。如果由于某种原因,您不想将字符串写入实际文件,您可以通过将字符串包装在 StringIO 中,让 pandas 认为您正在传递它。对象。

import pandas as pd
from io import StringIO

csv_string = '''
5/3 17,Astralis (16),FaZe (13),inferno,IEM Katowice 2017
5/3 17,Astralis (16),FaZe (12),nuke,IEM Katowice 2017
5/3 17,Astralis (16),FaZe (12),overpass,IEM Katowice 2017
5/3 17,FaZe (16),Astralis (9),cache,IEM Katowice 2017
4/3 17,Astralis (16),Heroic (12),nuke,IEM Katowice 2017
4/3 17,Astralis (16),Heroic (12),train,IEM Katowice 2017
4/3 17,Immortals (10),FaZe (16),mirage,IEM Katowice 2017
2/3 17,Virtus.pro (14),Heroic (16),nuke,IEM Katowice 2017
2/3 17,Cloud9 (6),Natus Vincere (16),mirage,IEM Katowice 2017
2/3 17,SK (16),North (8),cbble,IEM Katowice 2017
2/3 17,Cloud9 (12),North (16),cbble,IEM Katowice 2017
2/3 17,Natus Vincere (12),Heroic (16),overpass,IEM Katowice 2017
2/3 17,Virtus.pro (16),SK (14),inferno,IEM Katowice 2017
'''

csv_string_io = StringIO(csv_string)
frame = pd.read_csv(csv_string_file)

关于python - 从 CSV 输出制作 DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42795156/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com