zoukankan      html  css  js  c++  java
  • 【Python】Python读取非文本格式文件进行修改的问题

    A question not published(I solve it when I write) in 2017.3.20

    I just wanted to publish it to stackoverflow, but I solved it.

    1.Platform: windows 8
    I'm just trying to modify a file in python 2.7, which is not in plain text
    (like .txt, .csv) but docx format,in order to add one or more bytes to
    it.
    However, when I tried to read it through commands:
    with open('GGG.docx', 'rb+') as f:
    contents = f.read() # type(contents): str
    with open('GGG2.docx', 'rb+') as f:
    f.writelines(contents)
    , the result only appears to be write str to GGG2.docx, instead of bytes
    Format of GGG.docx:
        504b 0304 1400 0600 0800 0000 2100 c1f0
        070c 7801 0000 5a06 0000 1300 0802 5b43
        6f6e 7465 6e74 5f54 7970 6573 5d2e 786d
        6c20 a204 0228 a000 0200 0000 0000 0000
        0000 0000 0000 0000 0000 0000 0000 0000
    ...
    Format of GGG2.docx:
        504b 0304 1400 0600 0800 0000 2100 c1f0
        070c 7801 0000 5a06 0000 1300 0802 5b43
        6f6e 7465 6e74 5f54 7970 6573 5d2e 786d
        6c20 a204 0228 a000 0200 0000 0000 0000
        0000 0000 0000 0000 0000 0000 0000 0000
    They look the same in editor(sublime), but GGG2.docx is in fact
    the plain text '504B 0304 ...'


    2. Way 2:
    A similar question is 'http://stackoverflow.com/questions/21677646/python-writing-a-two-byte-string-as-a-single-byte-hex-character-to-a-binary-fi', but it doesn't work.


    3. Way 3:
    In http://stackoverflow.com/questions/12092527/python-write-bytes-to-file,
    a method was proposed like this:
        import io
        with open('GGG.docx', 'rb+') as f:
            contents = f.read() # type(contents): str
        with io.open('GGG2.docx', 'rb+') as f:
            f.writelines(contents)
    But in the link the user suggests it(open instead of io.open) is useful in windows.
    Now it works.....
    Thank you all the guys.
    Yep, I just want to give complaints to the designer of python that made this.

  • 相关阅读:
    Ambari源代码分析之总览
    最简单的修改HashMap value值的方法
    机器学习 Hidden Markov Models 1
    OpenCV坐标系与操作像素的四种方法
    OpenCV2.4.13+VS2012开发环境配置
    OpenCV——PS滤镜算法之 Ellipsoid (凹陷)
    OpenCV——PS滤镜算法之 Ellipsoid (凸出)
    如何快糙好猛的使用Shiqi.Yu老师的公开人脸检测库(附源码)
    伊斯坦布尔的流浪 (三)
    伊斯坦布尔的流浪 (一)
  • 原文地址:https://www.cnblogs.com/wangzming/p/6590862.html
Copyright © 2011-2022 走看看