<i id='VKS5R'><tr id='VKS5R'><dt id='VKS5R'><q id='VKS5R'><span id='VKS5R'><b id='VKS5R'><form id='VKS5R'><ins id='VKS5R'></ins><ul id='VKS5R'></ul><sub id='VKS5R'></sub></form><legend id='VKS5R'></legend><bdo id='VKS5R'><pre id='VKS5R'><center id='VKS5R'></center></pre></bdo></b><th id='VKS5R'></th></span></q></dt></tr></i><div id='VKS5R'><tfoot id='VKS5R'></tfoot><dl id='VKS5R'><fieldset id='VKS5R'></fieldset></dl></div>

        <bdo id='VKS5R'></bdo><ul id='VKS5R'></ul>

      <small id='VKS5R'></small><noframes id='VKS5R'>

      <tfoot id='VKS5R'></tfoot>
    1. <legend id='VKS5R'><style id='VKS5R'><dir id='VKS5R'><q id='VKS5R'></q></dir></style></legend>

        写一个“字符串"作为原始二进制文件放入文件 Python

        Write a quot;stringquot; as raw binary into a file Python(写一个“字符串作为原始二进制文件放入文件 Python)
        <legend id='GhAwm'><style id='GhAwm'><dir id='GhAwm'><q id='GhAwm'></q></dir></style></legend>
        1. <i id='GhAwm'><tr id='GhAwm'><dt id='GhAwm'><q id='GhAwm'><span id='GhAwm'><b id='GhAwm'><form id='GhAwm'><ins id='GhAwm'></ins><ul id='GhAwm'></ul><sub id='GhAwm'></sub></form><legend id='GhAwm'></legend><bdo id='GhAwm'><pre id='GhAwm'><center id='GhAwm'></center></pre></bdo></b><th id='GhAwm'></th></span></q></dt></tr></i><div id='GhAwm'><tfoot id='GhAwm'></tfoot><dl id='GhAwm'><fieldset id='GhAwm'></fieldset></dl></div>
        2. <tfoot id='GhAwm'></tfoot>

            • <bdo id='GhAwm'></bdo><ul id='GhAwm'></ul>

                <small id='GhAwm'></small><noframes id='GhAwm'>

                    <tbody id='GhAwm'></tbody>

                • 本文介绍了写一个“字符串"作为原始二进制文件放入文件 Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试编写一系列用于测试的文件,这些文件是我从头开始构建的.数据有效负载构建器的输出是字符串类型,我正在努力将字符串直接写入文件.

                  I'm trying to write a series of files for testing that I am building from scratch. The output of the data payload builder is of type string, and I'm struggling to get the string written directly to the file.

                  有效负载构建器仅使用十六进制值,并且只是为每次迭代添加一个字节.

                  The payload builder only uses hex values, and simply adds a byte for each iteration.

                  我尝试过的所有'write'函数都不是写字符串,就是写字符串的ASCII码,而不是字符串本身......

                  The 'write' functions I have tried all either fall over the writing of strings, or write the ASCII code for the string, rather than the string its self...

                  我想以一系列文件结束 - 与数据负载具有相同的文件名(例如,文件 ff.txt 包含字节 0xff

                  I want to end up with a series of files - with the same filename as the data payload (e.g. file ff.txt contains the byte 0xff

                  def doMakeData(counter):
                      dataPayload = "%X" %counter
                      if len(dataPayload)%2==1:
                          dataPayload = str('0') + str(dataPayload)
                      fileName = path+str(dataPayload)+".txt"
                      return dataPayload, fileName
                  
                  def doFilenameMaker(counter):
                      counter += 1
                      return counter
                  
                  def saveFile(dataPayload, fileName):
                      # with open(fileName, "w") as text_file:
                            # text_file.write("%s"%dataPayload)  #this just writes the ASCII for the string
                      f = file(fileName, 'wb')
                      dataPayload.write(f) #this also writes the ASCII for the string
                      f.close()
                      return
                  
                  if __name__ == "__main__":
                      path = "C:UsersmeDesktopoutput\"
                      counter = 0
                      iterator = 100
                      while counter < iterator:
                          counter = doFilenameMaker(counter)
                          dataPayload, fileName = doMakeData(counter)
                          print type(dataPayload)
                          saveFile(dataPayload, fileName)
                  

                  推荐答案

                  要只写入一个字节,请使用 chr(n) 获取包含整数 n 的字节.

                  To write just a byte, use chr(n) to get a byte containing integer n.

                  您的代码可以简化为:

                  import os
                  path = r'C:UsersmeDesktopoutput'
                  for counter in xrange(100):
                      with open(os.path.join(path,'{:02x}.txt'.format(counter)),'wb') as f:
                          f.write(chr(counter))
                  

                  注意路径使用原始字符串.如果字符串中有 ' ' 或 ' ' ,它们将被视为回车或换行,而不使用原始字符串.

                  Note use of raw string for the path. If you had a ' ' or ' ' in the string they would be treated as a carriage return or linefeed without using a raw string.

                  f.write 是写入文件的方法.chr(counter) 生成字节.确保也以二进制模式写入 'wb'.

                  f.write is the method to write to a file. chr(counter) generates the byte. Make sure to write in binary mode 'wb' as well.

                  这篇关于写一个“字符串"作为原始二进制文件放入文件 Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)

                    • <legend id='rCl0C'><style id='rCl0C'><dir id='rCl0C'><q id='rCl0C'></q></dir></style></legend>
                        <tbody id='rCl0C'></tbody>

                    • <tfoot id='rCl0C'></tfoot>
                          <bdo id='rCl0C'></bdo><ul id='rCl0C'></ul>
                        • <i id='rCl0C'><tr id='rCl0C'><dt id='rCl0C'><q id='rCl0C'><span id='rCl0C'><b id='rCl0C'><form id='rCl0C'><ins id='rCl0C'></ins><ul id='rCl0C'></ul><sub id='rCl0C'></sub></form><legend id='rCl0C'></legend><bdo id='rCl0C'><pre id='rCl0C'><center id='rCl0C'></center></pre></bdo></b><th id='rCl0C'></th></span></q></dt></tr></i><div id='rCl0C'><tfoot id='rCl0C'></tfoot><dl id='rCl0C'><fieldset id='rCl0C'></fieldset></dl></div>

                            <small id='rCl0C'></small><noframes id='rCl0C'>