gpt4 book ai didi

python - 在 Python 中创建空格分隔的文件

转载 作者:太空宇宙 更新时间:2023-11-04 10:53:43 25 4
gpt4 key购买 nike

使用下面的代码,我获得了文件的正确信息 - 有点像。这是代码:

filename = raw_input('What would you like to name the file? ')

import csv

data = [frames]
out = csv.writer(open(filename,"w"), delimiter=' ',quoting=csv.QUOTE_MINIMAL)
out.writerows(data)

这会在文本文件中生成如下所示的内容:

"[255   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0 0]" "[ 0 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171
171 171]"

我希望信息看起来像这样:

255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 []0 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

2 个括号代表一个字符,表示新的一行信息。它在我的文本编辑器上是一个小矩形,我不确定这个字符是什么。

那么,我该如何去除引号和括号并使用其他字符呢?

编辑:

我尝试使用下面的代码并得到以下错误:

Traceback (most recent call last):   
File "(stdin)", line 1, in (module)
File "backandforth3.py", line 154, in (module)
out.write(' '.join(frame))
Type Error: sequence item 0: expect string, numpy.int32 found

最佳答案

我假设您使用记事本作为文本编辑器? Unix 风格的换行符 (\n) 在记事本中显示为框,因为它只支持 Windows 换行符 (\r\n)。也就是说,这应该提供您期望的输出:

filename = raw_input('What would you like to name the file? ')

with open(filename, 'wb') as out:
for frame in frames:
out.write(' '.join(str(num) for num in frame))
out.write('\r\n')

没有理由将 frames 包装在另一个列表中;假设该变量是数字列表的列表,如预期输出所示,这应该可以正常工作。

此外,如果您绝对必须拥有输出中描述的“框”,请将 '\r\n' 替换为 '\n'

关于python - 在 Python 中创建空格分隔的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11697828/

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