0

我正在尝试使用 python 编辑一个 html 文本文件。打印时,它会给出一个空文件。为什么会变空?我试图打印它,因为我不知道如何退回它。 这是代码:

    import bleach 

    with open ('index1.txt','w') as f: #to open the file that contains html markups
    
            bleach.clean( 
             'f',
             tags=['p'],
             attributes=['style'],
             styles=['color'], 
            )    
    
    f=open('index1.txt')
    content = f.read()
    f.close()
    print(content)
4

2 回答 2

1

It becomes empty because you open file for writing with 'w' and thus make it empty as per documentation - just change it to 'r' or 'a'

于 2021-09-30T07:30:29.467 回答
0

它将保持为空,因为您只是在创建一个文件而不在上面写任何东西。

于 2021-09-30T07:39:30.160 回答