gpt4 book ai didi

python - 如何将抓取的数据写入调整好的列?

转载 作者:太空宇宙 更新时间:2023-11-04 01:20:06 25 4
gpt4 key购买 nike

我目前正在使用 BeautifulSoup 抓取数据,我正在尝试将抓取的数据写入一个文件,我想在其中制作经过良好调整的列。

这是我当前代码生成的结果:

1997       Fisrt Round         Sampras, Pete       Reneberg, Richey        4-6, 6-4, 6-3, 6-3
1997 Fisrt Round Philippoussis, Mark Frana, Javier 4-6, 6-4, 6-3, 6-3
1997 Fisrt Round Goossens, Kris Black, Byron 4-6, 6-4, 6-3, 6-3
...

我希望输出看起来像这样:

1997       Fisrt Round         Sampras, Pete             Reneberg, Richey        4-6, 6-4, 6-3, 6-3
1997 Fisrt Round Philippoussis, Mark Frana, Javier 4-6, 6-4, 6-3, 6-3
1997 Fisrt Round Goossens, Kris Black, Byron 4-6, 6-4, 6-3, 6-3
...

这是我的代码:

for tournamentYear_url in tournamentYear_urls:
i = 0
tournamentYear+=1
soup = make_soup(tournamentYear_url)
while i < len(get_player_name_first_round(soup)):
mon_fichier.write(str(tournamentYear) + " ")
mon_fichier.write(str("Fisrt Round "))
mon_fichier.write(get_player_name_first_round(soup)[i] + " ")
mon_fichier.write(get_player_name_first_round(soup)[i+1] + " ")
mon_fichier.write(get_player_score_first_round_winner(soup) + "\n")
i+=2

最佳答案

看看 Python 的 Format Specification Mini-Language .

有一部分专门关于文本对齐和字段宽度/填充:

'<' - Forces the field to be left-aligned within the available space (this is the default for most objects).

'>' - Forces the field to be right-aligned within the available space (this is the default for numbers).

'=' - Forces the padding to be placed after the sign (if any) but before the digits. This is used for printing fields in the form ‘+000000120’. This alignment option is only valid for numeric types.

'^' - Forces the field to be centered within the available space.

以及文档中有关如何使用 format() 的一些示例:

>>> '{:<30}'.format('left aligned')
'left aligned '
>>> '{:>30}'.format('right aligned')
' right aligned'
>>> '{:^30}'.format('centered')
' centered '
>>> '{:*^30}'.format('centered') # use '*' as a fill char
'***********centered***********'

关于python - 如何将抓取的数据写入调整好的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21812515/

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