繁体   English   中英

为什么我的代码仅在第二次运行时才记录到文件中?

[英]Why my code is recording into the file only when I run it second time?

我的目标是计算单词数。 当我运行代码时,我想:

  1. 从文件中读取字符串
  2. 用文字将每一行分开
  3. 将这些单词添加到字典中
  4. 排序键并将其添加到列表
  5. 将包含键和适当值的字符串写入文件

当我第一次运行的代码写在文件中任何东西,但我看到我的屏幕上的结果。 该文件为空。 仅当我第二次运行代码时,我看到内容被记录到文件中。

为什么会这样呢?

#read in the file
fileToRead = open('../folder/strings.txt')
fileToWrite = open('../folder/count.txt', 'w')

d = {}
#iterate over every line in the file
for line in fileToRead:
    listOfWords = line.split()
    #iterate over every word in the list
    for word in listOfWords:
        if word not in d:
            d[word] = 1
        else:
            d[word] = d.get(word) + 1
#sort the keys
listF = sorted(d)

#iterate over sorted keys and write them in the file with appropriate value
for word in listF:
    string = "{:<18}\t\t\t{}\n".format(word, d.get(word))
    print string
    fileToWrite.write(string)

进行了几次更改,它认为您的意思是每次打开时都将“ a”(附加到文件)而不是“ w”覆盖文件(“ count.txt”,“ a”)。 也请尝试使用with语句读取和写入文件,因为在完成读/写操作后,它将自动关闭文件描述符。

#read in the file
fileToRead = open('strings.txt')

d = {}
#iterate over every line in the file
for line in fileToRead:
    listOfWords = line.split()
    #iterate over every word in the list
    for word in listOfWords:
        if word not in d:
            d[word] = 1
        else:
            d[word] = d.get(word) + 1
#sort the keys
listF = sorted(d)

#iterate over sorted keys and write them in the file with appropriate value
with open('count.txt', 'a') as fileToWrite:
    for word in listF:
        string = "{:<18}\t\t\t{}\n".format(word, d.get(word))
        print string,
        fileToWrite.write(string)

当您执行file.write(some_data) ,它将数据写入缓冲区,但写入文件。 仅在执行file.close()时将文件保存到磁盘。

f = open('some_temp_file.txt', 'w')

f.write("booga boo!")
# nothing written yet to disk

f.close()
# flushes the buffer and writes to disk

更好的方法是将路径存储在变量中,而不是文件对象中。 然后,您可以根据需要打开文件(然后再次关闭)。

read_path = '../folder/strings.txt'
write_path = '../folder/count.txt'

这也使您可以使用with关键字,该关键字可以更优雅地处理文件的打开和关闭。

read_path = '../folder/strings.txt'
write_path = '../folder/count.txt'

d = dict()

with open(read_path) as inf:
    for line in inf:
        for word in line.split()
            d[word] = d.get(word, 0) + 1
            # remember dict.get's default value! Saves a conditional

# since we've left the block, `inf` is closed by here

sorted_words = sorted(d)

with open(write_path, 'w') as outf:
    for word in sorted_words:
        s = "{:<18}\t\t\t{}\n".format(word, d.get(word))
        # don't shadow the stdlib `string` module
        # also: why are you using both fixed width AND tab-delimiters in the same line?
        print(s)  # not sure why you're doing this, but okay...
        outf.write(s)
# since we leave the block, the file closes automagically.

就是说,您可以做一些事情来使总体情况更好一些。 首先:计算容器中有多少东西是collections.Counter的工作。

In [1]: from collections import Counter

In [2]: Counter('abc')
Out[2]: Counter({'a': 1, 'b': 1, 'c': 1})

Counter可以与预期行为一起添加

In [3]: Counter('abc') + Counter('cde')
Out[3]: Counter({'c': 2, 'a': 1, 'b': 1, 'd': 1, 'e': 1})

并以与键排序字典相同的方式进行排序

In [4]: sorted((Counter('abc') + Counter('cde')).items(), key=lambda kv: kv[0])
Out[4]: [('a', 1), ('b', 1), ('c', 2), ('d', 1), ('e', 1)]

将所有这些放在一起,您可以执行以下操作:

from collections import Counter

read_path = '../folder/strings.txt'
write_path = '../folder/count.txt'

with open(read_path) as inf:
    results = sum([Counter(line.split()) for line in inf])

with open(write_path, 'w') as outf:
    for word, count in sorted(results.items(), key=lambda kv: kv[0]):
        s = "{:<18}\t\t\t{}\n".format(word, count)
        outf.write(s)

简约版本:

import collections

with open('strings.txt') as f:
    d = collections.Counter(s for line in f for s in line.split())

with open('count.txt', 'a') as f:
    for word in sorted(d.iterkeys()):
        string = "{:<18}\t\t\t{}\n".format(word, d[word])
        print string,
        f.write(string)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM