为什么将HTML代码打印为字符串会给出十六进制数字作为python输出? [英] Why does printing html code as a string give hexadecimal numbers as output in python?

查看:76
本文介绍了为什么将HTML代码打印为字符串会给出十六进制数字作为python输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个Python代码来修改我的html内容.但是在再次将其写入html文件时,我得到了奇怪的十六进制数字

I wrote a Python code to modify my html contents. But on writing that again to the html file, I get weird hexadecimal numbers

import re

search="www.abc.com"

description="blah blah"

f = open('myhtml.html','r+')
content = f.read()
exp_keyword = re.compile(r'\.(\S+)\.')
reducedSearch = exp_keyword.findall(search)[0]

regexLink = re.compile(reducedSearch+r'\.'+r'.+'+'</a>',re.DOTALL)
matchregexLink = regexLink.search(content)
endOfMatch = matchregexLink.span()[1]   

#slice the string
s1 = content[:endOfMatch]
s2=content[endOfMatch:]

content = s1+description+s2
print(content)
f.truncate(0)
f.write(content)

<html>
 <head>
 </head>
 <body>
  <div id="phy">
   <p>
    ett
   </p>
   <div class="links">
    <ul>
     <a href="www.abcd.com">
      Link
     </a>
     <a href="www.abc.com">
      Link
     </a>
    </ul>
   </div>
  </div>
 </body>
</html>

0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 003c 6874 6d6c 3e0a
203c 6865 6164 3e0a 203c 2f68 6561 643e
0a20 3c62 6f64 793e 0a20 203c 6469 7620
6964 3d22 7068 7922 3e0a 2020 203c 703e
0a20 2020 2065 7474 0a20 2020 3c2f 703e
0a20 2020 3c64 6976 2063 6c61 7373 3d22
6c69 6e6b 7322 3e0a 2020 2020 3c75 6c3e
0a20 2020 2020 3c61 2068 7265 663d 2277
7777 2e61 6263 642e 636f 6d22 3e0a 2020
2020 2020 4c69 6e6b 0a20 2020 2020 3c2f
613e 0a20 2020 2020 3c61 2068 7265 663d
2277 7777 2e61 6263 2e63 6f6d 223e 0a20
2020 2020 204c 696e 6b0a 2020 2020 203c
2f61 3e62 6c61 6820 626c 6168 0a20 2020
203c 2f75 6c3e 0a20 2020 3c2f 6469 763e
0a20 203c 2f64 6976 3e0a 203c 2f62 6f64
793e 0a3c 2f68 746d 6c3e 0a

这些奇怪的十六进制数字是我作为输出得到的.但是,当我在代码中打印content时,它给出了正确的答案.为什么这样? 我的预期答案是在包含www.abc.com链接的</a>结束标记后写的blah blah.

These weird hexadecimal numbers is what I get as output. However, when I print content in the code, it gives correct answer. Why so? My expected answer is blah blah written after the closing </a> tag containing the www.abc.com link.

推荐答案

截断文件时,实际上并没有寻找回文件中的位置0.文件位置仍然是原来的位置,现在已经超过文件末尾了.

When you truncated your file, you didn't actually seek back to position 0 in the file. The file position is still where it was, which is now past the end of the file.

写入文件会将文本写在旧文件位置,而该位置之前的所有内容都保留为空白.

Writing to the file writes the text at the old file position, leaving everything before that point blank.

十六进制不是由您发布的代码中的任何内容引起的.您已经在一个工具中打开了该文件,该工具向您显示了字节的原始十六进制值.由于所有空字节,此工具可能正在执行此操作.

The hex isn't caused by anything in the code you've posted. You've opened the file in a tool that's showing you the raw hex values of the bytes. This tool might be doing that because of all the null bytes.

这篇关于为什么将HTML代码打印为字符串会给出十六进制数字作为python输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆