在我的文本文件 python 顶部创建的空行

凯勒博萨
myfile = open('curr_data.txt', 'w')
for row in stat_table.find_all('tr'):           
    for cell in row.find_all('td'):
        myfile.write(cell.text + ',')
    myfile.write("\n")

当它写入文本文件时,它会在文本文件的顶部添加一个空行。这是由于“\ n”引起的某种原因,因为当我删除它时,没有空行。

就好像myfile.write("\n")之前被执行过一样myfile.write(cell.text + ',')

我的输出看起来像

                BLANK LINE HERE
1#BitcoinBTC#ZAR108,103.18#+0.16%#ZAR2.0T#Trade#
2#EthereumETH#ZAR2,148.71#+0.03%#ZAR233.8B#Trade#
3#XRPXRP#ZAR3.25#+2.40%#ZAR140.6B#Trade#
4#Bitcoin CashBCH#ZAR3,093.58#-0.92%#ZAR56.2B#Trade#
5#LitecoinLTC#ZAR652.31#-1.14%#ZAR41.6B#Trade#

我希望它看起来像的地方

1#BitcoinBTC#ZAR108,103.18#+0.16%#ZAR2.0T#Trade#
2#EthereumETH#ZAR2,148.71#+0.03%#ZAR233.8B#Trade#
3#XRPXRP#ZAR3.25#+2.40%#ZAR140.6B#Trade#
4#Bitcoin CashBCH#ZAR3,093.58#-0.92%#ZAR56.2B#Trade#
5#LitecoinLTC#ZAR652.31#-1.14%#ZAR41.6B#Trade#
赛斯

通常,html 表的起始行带有标题 ( th)。

如果你保证总是有一行带有表头,那么你可以从第一个索引开始你的外循环开始切片。

for row in stat_table.find_all('tr')[1:]:

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章