gpt4 book ai didi

python - (Python 3)如何在不先保存的情况下将二进制文件作为文本传递

转载 作者:行者123 更新时间:2023-11-28 17:17:16 25 4
gpt4 key购买 nike

或者,也许是一个更好的标题:如何在将二进制文件传递给文本模式写入子句时避免不需要的额外回车。

Python 3.6, window 。输入文件需要首先进行二进制搜索/替换,然后进行正则表达式搜索/替换。

我首先以二进制模式打开输入文件,完成工作,然后以二进制模式将其保存在一个临时文件中。然后我以文本模式打开它,进行正则表达式搜索/替换,并以文本模式保存它(名称类似于输入文件的名称)。

def fixbin(infile): 
with open(infile, 'rb') as f:
file = f.read()

# a few bytearray operations here, then:
with open('bin.tmp', 'wb') as f:
f.write(file)

def fix4801(fname, ext):
outfile = '{}_OK{}'.format(fname, ext)
with open('bin.tmp', encoding='utf-8-sig', mode='r') as f, \
open(outfile, encoding='utf-8-sig', mode='w') as g:
infile = f.read()
x = re.sub(r'(\n4801.+\n)4801', r'\1 ', infile)
g.write(y)

infile, fname, ext = get_infile() # function get_infile not shown for brevity
fixbin(infile)
fix4801(fname, ext)

它有效,但它很丑。我宁愿将输出作为文件传递,如下所示:

def fixbin(infile): 
with open(infile, 'rb') as f:
file = f.read()
# a few bytearray operations here, and then
return file.decode('utf-8')

def fix4801(infile):
x = re.sub(r'(\n4801.+\n)4801', r'\1 ', infile)
return x

...
temp = fixbin(infile)
result = fix4801(temp)

outfile = '{}_OK{}'.format(fname, ext)
with open(outfile, encoding='utf-8-sig', mode='w') as g:
g.write(result)

但是随后输出文件 (Windows) 得到了不需要的额外回车符。症状描述here ,但原因不同:我没有使用 os.linesep,换句话说,我的代码中没有 os.linesep。 (底层库可能有,我没查)

我做错了什么?

最佳答案

Python » Documentation : open

open(file, mode='r', buffering=-1, encoding=None, errors=None, 
newline=None, closefd=True, opener=None)

默认:newline=None如果换行符是'''\n' , 没有翻译发生。
如果有任何不同,请尝试以下操作:

#change
open(outfile, encoding='utf-8-sig', mode='w') as g:
#with
open(outfile, encoding='utf-8-sig', mode='w', newline='') as g:

Question: ... there is no os.linesep in my code.


Python » Documentation : open
When writing output to the stream, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep. If newline is '' or '\n', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string.

关于python - (Python 3)如何在不先保存的情况下将二进制文件作为文本传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43528959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com