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.

  • 相关阅读:
    转:C/C++基本数据类型所占字节数
    转:为什么C++中空类和空结构体大小为1?
    转:内存字节对齐
    转:100层楼扔两个鸡蛋的问题
    移动互联网
    辗转相除法
    斐波那契数列
    error C2783: 无法为“T”推导 模板 参数
    创新工场和海豚浏览器宣讲会启示
    转:快速排序的一个小问题没想明白,求助各位
  • 原文地址:https://www.cnblogs.com/wangzming/p/6590862.html
Copyright © 2011-2022 走看看