如何在文件末尾循环打印 globals() (具有其他写日志命令)?

How to pprint globals() in a loop (that has another write log command) at end of file?

我正在尝试创建一个日志文件,该文件使用 pprint globals() 接收变量信息并写入文件。但是因为我必须使用很多循环,有没有办法在日志文件末尾的每个循环中为下面显示的代码放置所有 pprint globals() 输出:

import numpy as np
from pprint import pprint
A = np.array([1, 2, 3, 4])
f = open("log.txt", 'w')
n = 3
for i in range(n):
    f.write(u'\u27A4 - %s\n'.encode('utf-8') % str(i))
    A = A + 1
    f.writelines(list(u'    \u27B3 - %s\n'.encode('utf-8') % i for i in A))
    pprint(globals(), f)
f.close()

输出

➤ - 0
    ➳ - 2
    ➳ - 3
    ➳ - 4
    ➳ - 5
{'A': array([2, 3, 4, 5]),
 '__builtins__': <module '__builtin__' (built-in)>,
 '__doc__': None,
 '__file__': '~/Stack exchange/pprint_global.py',
 '__name__': '__main__',
 '__package__': None,
 'f': <open file 'log.txt', mode 'w' at 0xb66588b8>,
 'i': 0,
 'n': 2,
 'np': <module 'numpy' from '/usr/lib/python2.7/dist-packages/numpy/__init__.pyc'>,
 'pprint': <function pprint at 0xb6e2748c>}
➤ - 1
    ➳ - 3
    ➳ - 4
    ➳ - 5
    ➳ - 6
{'A': array([3, 4, 5, 6]),
 '__builtins__': <module '__builtin__' (built-in)>,
 '__doc__': None,
 '__file__': '~/Stack exchange/pprint_global.py',
 '__name__': '__main__',
 '__package__': None,
 'f': <open file 'log.txt', mode 'w' at 0xb66588b8>,
 'i': 1,
 'n': 2,
 'np': <module 'numpy' from '/usr/lib/python2.7/dist-packages/numpy/__init__.pyc'>,
 'pprint': <function pprint at 0xb6e2748c>}

期望输出

➤ - 0
    ➳ - 2
    ➳ - 3
    ➳ - 4
    ➳ - 5
➤ - 1
    ➳ - 3
    ➳ - 4
    ➳ - 5
    ➳ - 6
{'A': array([2, 3, 4, 5]),
 '__builtins__': <module '__builtin__' (built-in)>,
 '__doc__': None,
 '__file__': '~/Stack exchange/pprint_global.py',
 '__name__': '__main__',
 '__package__': None,
 'f': <open file 'log.txt', mode 'w' at 0xb66588b8>,
 'i': 0,
 'n': 2,
 'np': <module 'numpy' from '/usr/lib/python2.7/dist-packages/numpy/__init__.pyc'>,
 'pprint': <function pprint at 0xb6e2748c>}
{'A': array([3, 4, 5, 6]),
 '__builtins__': <module '__builtin__' (built-in)>,
 '__doc__': None,
 '__file__': '~/Stack exchange/pprint_global.py',
 '__name__': '__main__',
 '__package__': None,
 'f': <open file 'log.txt', mode 'w' at 0xb66588b8>,
 'i': 1,
 'n': 2,
 'np': <module 'numpy' from '/usr/lib/python2.7/dist-packages/numpy/__init__.pyc'>,
 'pprint': <function pprint at 0xb6e2748c>}

第一印象是将pprint命令放在代码的末尾,但这不会给我每次循环期间的变量信息。它只会在循环结束时给我值。简而言之,我需要在每个循环(有另一个写日志命令)写入日志文件末尾的变量信息。

一个简单的方法是将输出写入两个单独的文本文件,然后在代码本身中连接这些文件。您也可以通过更改 log_file_list 选择连接文件的顺序。使用上述方法使用您的代码:

import numpy as np
from pprint import pprint
A = np.array([1, 2, 3, 4])
f1 = open("log1.txt", 'w')
f2 = open("log2.txt", 'w')
n = 2
for i in range(n):
    f1.write(u'\u27A4 - %s\n'.encode('utf-8') % str(i))
    A = A + 1
    f1.writelines(list(u'    \u27B3 - %s\n'.encode('utf-8') % i for i in A))
    pprint(globals(), f2)
f1.close()
f2.close()
log_file_list = ['log1.txt', 'log2.txt']
with open('log.txt', 'w') as output_file:
    for log_file in log_file_list:
        with open(log_file) as input_file:
            for line in input_file:
                output_file.write(line)
        output_file.write('\n')

替代方法

不使用行的替代代码:

import numpy as np
from pprint import pprint
A = np.array([1, 2, 3, 4])
f1 = open("log1.txt", 'w')
f2 = open("log2.txt", 'w')
n = 2
for i in range(n):
    f1.write(u'\u27A4 - %s\n'.encode('utf-8') % str(i))
    A = A + 1
    f1.writelines(list(u'    \u27B3 - %s\n'.encode('utf-8') % i for i in A))
    pprint(globals(), f2)
f1.close()
f2.close()
f1 = open("log1.txt", 'r')
f2 = open("log2.txt", 'r')
f = open("log.txt", 'w')
f.write(f1.read())
f.write(f2.read())
f.close()