我正在尝试解析 BMP 文件,进行一些更改,然后使用 Python 重新组装新的 BMP。
回车似乎是个大问题。当我使用 Notepad++ 打开位图文件并搜索 "\r'
时,该字符不存在。我在 Python 中读取该文件 (readData = fileIn.read()) 并尝试使用 "\n"
效果很好。
>readData.find('\r') 它返回 -1
。目前 尝试将完全相同的文本块写入新的使用 fileOut.write(readData)
的 BMP 并使用 Notepad++ 搜索 "\r"
,我能够找到它(两次,每次对应于预先存在的 "\n"
字符)。
有没有办法在不自动添加 "\r"
的情况下将此数据块写入新的 BMP? .strip() 和在将字符串写入新文件之前将 .replace('\r','')
替换为该字符串。
I'm trying to parse a BMP file, do some changes and then reassemble a new BMP using Python.
The carriage return seems to be a huge problem. When I open the bitmap file using Notepad++ and search for "\r'
, the character does not exist. I read the file in Python (readData = fileIn.read()) and try searching using readData.find('\r')
it returns -1
. Searching for "\n"
works fine. All is good for now.
When I try to write this exact same block of text into a new BMP using fileOut.write(readData)
and I use Notepad++ to search for "\r"
, I am able to find it (twice, each corresponding to the preexisting "\n"
characters).
Is there a way to write this block of data to a new BMP without "\r"
being added automatically? I've tried applying .strip()
and .replace('\r','')
to the string before writing it to the new file.
发布评论
评论(2)
当您想以二进制方式打开文件时,您可能会以文本方式打开文件(默认)。
来自文档:
You're probably opening the file as text (the default) when you want to open it as binary.
From the documentation:
您正在以文本模式打开文件,而您需要二进制模式。在这里找到有关 open() 的更多信息:
http://docs.python.org/library/functions.html
You are opening the file in text mode, while you need binary mode. Find more about open() here:
http://docs.python.org/library/functions.html