如何将输出保存到txt文件?

时间:2021-09-17 13:55:42

When I got the number of how many times of the words, I wanted to save the output to a txt file. But when I used the following code, only counts appeared in the output file. Anyone knows the problem here? Thank you very much!

当我得到单词的次数时,我想将输出保存为txt文件。但是当我使用以下代码时,只有计数出现在输出文件中。谁知道这里的问题?非常感谢你!

My code: (part)

我的代码:(部分)

d = c.split() # make a string into a list of words
#print d
counts = Counter(d) # count the words 
print(counts)

import sys
sys.stdout = open("C:/Users/Administrator/Desktop/out.txt", "w")
print 'counts'

4 个解决方案

#1


import sys
from collections import Counter

c = "When I got the number of how many times of the words"
d = c.split() # make a string into a list of words
counts = Counter(d) # count the words 
sys.stdout = open("C:/Users/Administrator/Desktop/out.txt", "w")
print(str(len(d)) + ' words') #this shows the number of total words 
for i in counts:
    print(str(i), str(counts[i]) + ' counts')

With te result in out.txt

用te结果out.txt

12 words
When 1 counts
got 1 counts
many 1 counts
times 1 counts
the 2 counts
words 1 counts
I 1 counts
number 1 counts
how 1 counts
of 2 counts     

#2


Working as intended as python is a dynamic language and everything as it was during runtime. So in order to capture everything you will have to redirect your stdout at the begging of your script.

像python一样工作是一种动态语言,它在运行时是一样的。因此,为了捕获所有内容,您必须在脚本的乞讨时重定向stdout。

import sys
sys.stdout = open("C:/Users/Administrator/Desktop/out.txt", "w")
d = c.split() # make a string into a list of words
#print d
counts = Counter(d) # count the words 
print(counts)

print 'counts'

#3


import sys
d = c.split() # make a string into a list of words
#print d
counts = Counter(d) # count the words 
sys.stdout = open("C:/Users/Administrator/Desktop/out.txt", "w")
print(counts)
print 'counts'

print prints to stdout. You need to redirrect stdout to the file before printing the number of counts.

打印到stdout。在打印计数之前,您需要将stdout重新写入文件。

#4


Another, slightly nicer option, would be to use some of the newer Python features:

另一个更好的选择是使用一些较新的Python功能:

from __future__ import print_function

with open(r'c:\Users\Administrator\Desktop\out.txt', 'w') as f:
    d = c.split() # make a string into a list of words
    #print(d, file=f)
    counts = Counter(d) # count the words 
    print(counts, file=f)

    print('counts', file=f)

Alternatively, you could use the logging module:

或者,您可以使用日志记录模块:

import logging

logger = logging.getLogger('mylogger')
logger.addHandler(logging.FileHandler(filename=r'c:\Users\Administrator\Desktop\out.txt'))
logger.setLevel(logging.DEBUG)


wordlist = c.split()
logger.debug('Wordlist: %s', wordlist)

logger.debug('Counting words..')
counts = Counter(wordlist)
logger.debug('Counts: %s', counts)

As it appears you're on Windows, you can use the incredibly useful baretail application to watch your log as your program executes.

由于您似乎在Windows上,因此您可以使用非常有用的裸尾应用程序在程序执行时查看日志。

#1


import sys
from collections import Counter

c = "When I got the number of how many times of the words"
d = c.split() # make a string into a list of words
counts = Counter(d) # count the words 
sys.stdout = open("C:/Users/Administrator/Desktop/out.txt", "w")
print(str(len(d)) + ' words') #this shows the number of total words 
for i in counts:
    print(str(i), str(counts[i]) + ' counts')

With te result in out.txt

用te结果out.txt

12 words
When 1 counts
got 1 counts
many 1 counts
times 1 counts
the 2 counts
words 1 counts
I 1 counts
number 1 counts
how 1 counts
of 2 counts     

#2


Working as intended as python is a dynamic language and everything as it was during runtime. So in order to capture everything you will have to redirect your stdout at the begging of your script.

像python一样工作是一种动态语言,它在运行时是一样的。因此,为了捕获所有内容,您必须在脚本的乞讨时重定向stdout。

import sys
sys.stdout = open("C:/Users/Administrator/Desktop/out.txt", "w")
d = c.split() # make a string into a list of words
#print d
counts = Counter(d) # count the words 
print(counts)

print 'counts'

#3


import sys
d = c.split() # make a string into a list of words
#print d
counts = Counter(d) # count the words 
sys.stdout = open("C:/Users/Administrator/Desktop/out.txt", "w")
print(counts)
print 'counts'

print prints to stdout. You need to redirrect stdout to the file before printing the number of counts.

打印到stdout。在打印计数之前,您需要将stdout重新写入文件。

#4


Another, slightly nicer option, would be to use some of the newer Python features:

另一个更好的选择是使用一些较新的Python功能:

from __future__ import print_function

with open(r'c:\Users\Administrator\Desktop\out.txt', 'w') as f:
    d = c.split() # make a string into a list of words
    #print(d, file=f)
    counts = Counter(d) # count the words 
    print(counts, file=f)

    print('counts', file=f)

Alternatively, you could use the logging module:

或者,您可以使用日志记录模块:

import logging

logger = logging.getLogger('mylogger')
logger.addHandler(logging.FileHandler(filename=r'c:\Users\Administrator\Desktop\out.txt'))
logger.setLevel(logging.DEBUG)


wordlist = c.split()
logger.debug('Wordlist: %s', wordlist)

logger.debug('Counting words..')
counts = Counter(wordlist)
logger.debug('Counts: %s', counts)

As it appears you're on Windows, you can use the incredibly useful baretail application to watch your log as your program executes.

由于您似乎在Windows上,因此您可以使用非常有用的裸尾应用程序在程序执行时查看日志。