0

我正在尝试创建一个日志文件,该文件使用 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 命令放在代码的末尾,但这不会在每个循环期间为我提供变量信息。它只会在循环结束时给我价值。简而言之,我需要将每个循环(有另一个写入日志命令)写入日志文件末尾的变量信息。

4

1 回答 1

1

一种简单的方法是将输出写入两个单独的文本文件,然后在代码本身中连接文件。您还可以通过更改 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()
于 2015-03-09T04:59:17.213 回答