打印到.txt
函数时出现一些问题。有很多可用的选项,我已经尝试了几种方法,但我遇到了错误。
我创建了一个数独求解器,它从文本文件中读取9×9个数组,并求解所有数组。我有一个工作函数,可以将它们打印到终端中,但我需要将结果输出到文本文件中,并按终端中的格式进行格式化。
有什么方法可以修改下面的打印功能来实现这一点吗?我有一个半工作的单独函数,它输出到.txt
,但使用的是未格式化的方法,并且是我一直使用的四个谜题数组中的一个。
谢谢你的帮助。
def print_board(board):
for x in range(len(board)):
if x % 3 == 0 and x != 0:
print("- - - - - - - - - ")
for y in range(len(board[0])):
if y % 3 == 0 and y != 0:
print("| ", end="")
if y == 8:
print(board[x][y])
else:
print(str(board[x][y]) + " ", end="")
return
这是只显示一个数组的导出函数。
def export_board(board):
outputfile=open('solvedpuzzles.txt', 'w')
print(output, file=outputfile)
outputfile.close()
这是我需要实现的格式,目前正在print_board
的终端中工作
4 8 3 | 9 2 1 | 6 5 7
9 6 7 | 3 4 5 | 8 2 1
2 5 1 | 8 7 6 | 4 9 3
- - - - - - - - -
5 4 8 | 1 3 2 | 9 7 6
7 2 9 | 5 6 4 | 1 3 8
1 3 6 | 7 9 8 | 2 4 5
- - - - - - - - -
3 7 2 | 6 8 9 | 5 1 4
8 1 4 | 2 5 3 | 7 6 9
6 9 5 | 4 1 7 | 3 8 2
编辑:我做了一些调整,并有这个:
def print_board(boards):
output = ''
with open("puzzleoutput.txt", "a") as file:
for x in range(len(board)):
if x % 3 == 0 and x != 0:
output += (str("n - - - - - - - - - "))
for y in range(len(board[0])):
if y % 3 == 0 and y != 0:
output += (str(" | "))
if y == 8:
output += (str(board[x][y]))
else:
output += (str(f"{board[x][y]} "))
file.write(str(output))
return
有人能告诉我如何为单独的9行添加新行吗?它打印出这样的阵列,我想我几乎已经破解了
4 8 3 | 9 2 1 | 6 5 79 6 7 | 3 4 5 | 8 2 12 5 1 | 8 7 6 | 4 9 3
- - - - - - - - - 5 4 8 | 1 3 2 | 9 7 67 2 9 | 5 6 4 | 1 3 8
- - - - - - - - - 3 7 2 | 6 8 9 | 5 1 48 1 4 | 2 5 3 | 7 6 9
您不能简单地用writelines([])
语句替换print()
语句吗?
output = []
with open("demofile3.txt", "a") as file:
for x in range(len(board)):
if x % 3 == 0 and x != 0:
output.append("- - - - - - - - - ")
for y in range(len(board[0])):
if y % 3 == 0 and y != 0:
output.append("| ")
if y == 8:
output.append(board[x][y])
else:
output.append(f"{board[x][y]} ")
file.writelines(output)
前言
当我在网上搜索已解决的数独时,这比搜索数独要难得多。在这里我找到了我想要的东西,所以谢谢你发布一个。
这就是我想要回报的,无论是对你,还是对有一天偶然发现这个问题的其他人。
打印到文件
内置Python3的print
函数有一个可选参数file
,对于您已经知道的print
调用,它默认为sys.stdout
。解决问题的最佳方法是通过print_board
函数向print
传递一个(也是可选的(参数。以下脚本包含函数的修改版本。它不仅显示了print
函数的file
参数的用法,还消除了由于在外循环中处理行而将x
和y
混淆的缺陷(如果我们将x
作为水平坐标,y
作为垂直坐标(。这个脚本还包含一个函数,可以将您定义为输出的内容转换为一个板(我懒得键入嵌套的python列表(:
# The optional `output` parameter specifies an
# open file; leave out to print to the console
def print_board(board, output=None):
for y in range(len(board)):
if y % 3 == 0 and y != 0:
print("- - - - - - - - - ", file=output)
for x in range(len(board[0])):
if x % 3 == 0 and x != 0:
print("| ", end="", file=output)
if x == 8:
print(board[y][x], file=output)
else:
print(str(board[y][x]) + " ", end="", file=output)
# Read a bord from a string that was formatted by
# the `print_board` function
def text_to_board(text):
text = text.strip()
board = list()
for line in text.split('n'):
if '-' not in line:
row = list()
for letter in line.split():
if letter != '|':
row.append(letter)
board.append(row)
return board
text = """
4 8 3 | 9 2 1 | 6 5 7
9 6 7 | 3 4 5 | 8 2 1
2 5 1 | 8 7 6 | 4 9 3
- - - - - - - - -
5 4 8 | 1 3 2 | 9 7 6
7 2 9 | 5 6 4 | 1 3 8
1 3 6 | 7 9 8 | 2 4 5
- - - - - - - - -
3 7 2 | 6 8 9 | 5 1 4
8 1 4 | 2 5 3 | 7 6 9
6 9 5 | 4 1 7 | 3 8 2
"""
board = text_to_board(text)
print_board(board)
with open('puzzleoutput.txt', 'w') as f:
print_board(board, f)
性能注意事项
通常情况下,直接写入文件是很有效的,我觉得它足够快。因此,我不建议构建一个一次性编写的文本缓冲区:字符串连接,尤其是逐字符连接,需要大量的内存分配和释放操作。
如果性能真的很重要,我建议不仅重新设计print_board
功能,还重新设计板存储本身。当然,还有关于如何衡量性能的扎实知识。
进一步阅读
打印到文件
print
in:内置函数——Python 3.10.4文档- 如何将"打印"输出重定向到文件?及其答案