Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在我的文本文件python顶部创建的空行_Python_Text Files - Fatal编程技术网

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

在我的文本文件python顶部创建的空行,python,text-files,Python,Text Files,当它写入文本文件时,会在文本文件的顶部添加一个空行。这是由“\n”引起的,因为当我删除它时,没有空行 这就好像在myfile.write(cell.text+,')之前执行myfile.write(“\n”) 我的输出看起来像 myfile = open('curr_data.txt', 'w') for row in stat_table.find_all('tr'): for cell in row.find_all('td'): myfile.

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

这就好像在
myfile.write(cell.text+,')之前执行
myfile.write(“\n”)

我的输出看起来像

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")
我想让它看起来像什么

                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#

只要检查任何
行。find_all('td')
是否返回空列表(即
tr
不带
td
),如果是空列表,请避免
myfile.write(“\n”)


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

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

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


统计表中是否有一行没有任何单元格?通常,html表格有一个带有标题的起始行(
th
)是的,现在我看到表格的标题中有一行没有任何td。有没有办法跳过循环中的第一行@请举例说明,但不要在任何输入/输出中发布原始信息。
myfile = open('curr_data.txt', 'w')
for row in stat_table.find_all('tr'): 
    data = row.find_all('td')
    if data:        
        for cell in data:
            myfile.write(cell.text + ',')
        myfile.write("\n")

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