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

时间:2015-07-09 18:10:06

标签: python

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

  1. 从文件中读取字符串
  2. 将每一行分成单词
  3. 将这些单词添加到词典中
  4. 对键进行排序并将其添加到列表中
  5. 将包含键和适当值的字符串写入文件
  6. 当我第一次运行代码时,在文件中写入任何内容,但我在屏幕上看到了结果。该文件为空。只有当我第二次运行代码时,才会看到内容被记录到文件中。

    为什么会这样?

    #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)
    

3 个答案:

答案 0 :(得分:0)

情侣改变,它认为每次打开('count.txt','a')时你的意思是'a'(附加到文件)而不是'w'覆盖文件。还请尝试使用with语句来读取和写入文件,因为它会在读/写完成后自动关闭文件描述符。

DELIMITER $$

DROP PROCEDURE IF EXISTS sp_AddProduct$$

create procedure sp_AddProduct (
  IN PostDate datetime,
  IN PostParent int,
  IN SKU varchar(10),
  IN Weight int,
  IN Price Decimal(5,2),
  IN Size varchar(10),
  IN GUID varchar(250),
  IN MenuOrder int
)
BEGIN
  DECLARE PostID int;

  INSERT INTO wp_posts (
    post_author,
    post_date, 
    post_date_gmt, 
    post_content, 
    post_title, 
    post_excerpt, 
    post_status, 
    comment_status, 
    ping_status, 
    post_password, 
    post_name, 
    to_ping, 
    pinged, 
    post_modified, 
    post_modified_gmt, 
    post_content_filtered, 
    post_parent, 
    guid, 
    menu_order, 
    post_type, 
    post_mime_type, 
    comment_count
  ) 
  VALUES (
    '1',
    PostDate,
    PostDate,
    '',
    '',
    '',
    'publish',
    'open',
    'open',
    '',
    '',
    '',
    '',
    PostDate,
    PostDate,
    '',
    PostParent,
    GUID,
    MenuOrder,
    'product_variation',
    '',
    0
  );/*$$*/

  /* Set the PostID = the PostID generated from the above insert*/
  -- DECLARE PostID int$$
  set PostID = LAST_INSERT_ID();/*$$*/    

  INSERT INTO wp_postmeta(post_id, meta_key, meta_value) VALUES (PostID,'_sku',SKU);/*$$*/
  INSERT INTO wp_postmeta(post_id, meta_key, meta_value) VALUES (PostID,'_thumbnail_id',0);/*$$*/
  INSERT INTO wp_postmeta(post_id, meta_key, meta_value) VALUES (PostID,'_virtual','no');/*$$*/
  INSERT INTO wp_postmeta(post_id, meta_key, meta_value) VALUES (PostID,'_downloadable','no');/*$$*/
  INSERT INTO wp_postmeta(post_id, meta_key, meta_value) VALUES (PostID,'_weight',Weight);/*$$*/
  INSERT INTO wp_postmeta(post_id, meta_key, meta_value) VALUES (PostID,'_length','');/*$$*/
  INSERT INTO wp_postmeta(post_id, meta_key, meta_value) VALUES (PostID,'_width','');/*$$*/
  INSERT INTO wp_postmeta(post_id, meta_key, meta_value) VALUES (PostID,'_height','');/*$$*/
  INSERT INTO wp_postmeta(post_id, meta_key, meta_value) VALUES (PostID,'_manage_stock','no');/*$$*/
  INSERT INTO wp_postmeta(post_id, meta_key, meta_value) VALUES (PostID,'_stock_status','instock');/*$$*/
  INSERT INTO wp_postmeta(post_id, meta_key, meta_value) VALUES (PostID,'_regular_price',Price);/*$$*/
  INSERT INTO wp_postmeta(post_id, meta_key, meta_value) VALUES (PostID,'_sale_price','');/*$$*/
  INSERT INTO wp_postmeta(post_id, meta_key, meta_value) VALUES (PostID,'_sale_price_dates_from','');/*$$*/
  INSERT INTO wp_postmeta(post_id, meta_key, meta_value) VALUES (PostID,'_sale_price_dates_to','');/*$$*/
  INSERT INTO wp_postmeta(post_id, meta_key, meta_value) VALUES (PostID,'_price',Price);/*$$*/
  INSERT INTO wp_postmeta(post_id, meta_key, meta_value) VALUES (PostID,'_download_limit','');/*$$*/
  INSERT INTO wp_postmeta(post_id, meta_key, meta_value) VALUES (PostID,'_download_expiry','');/*$$*/
  INSERT INTO wp_postmeta(post_id, meta_key, meta_value) VALUES (PostID,'_downloadable_files','');/*$$*/
  INSERT INTO wp_postmeta(post_id, meta_key, meta_value) VALUES (PostID,'_attribute_pa_size',Size);/*$$*/
END$$

DELIMITER ;

答案 1 :(得分:0)

执行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)

答案 2 :(得分:0)

简约版:

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)