并将该文本文件读入字典

并将该文本文件读入字典

本文介绍了Python:将字典写入文本文件(CSV?),并将该文本文件读入字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手的Python程序员,所以答案可能很简单。但是,我花了好几天时间研究了这个单一的问题,我被困了。这也是我第一个Stack Overflow帖子,所以如果我在这里打破一些规则,请放心。 :)



使用Python,我想将多行中的两个字段写入一个新的文本文件,并在稍后的运行中将该文本文件读入一个新的字典以引用以前的值。



我正在测试我在这里找到的代码:。所提供的解决方案有64个投票,并被标记为提问者的最佳答案。所以对大多数人来说似乎是正确的。



我的测试代码成功地创建了一个初始字典 mydict 并将其保存到一个文本文件中。然而,当我尝试将文本文件读回到一个新的字典 mydict2 时,我得到...没有什么,没有什么,没有错误信息或任何东西,没有线索,什么也没有。 / p>

我在这里缺少什么?



这是我的测试代码

  import csv 

mydict = {1:11,2:22,3:33}
打印mydict
打印mydict [1]

writer = csv.writer(open('dict.csv','wb'))
为键,mydict中的值。 items():
writer.writerow([key,value])

reader = csv.reader(open('dict.csv','rb'))
mydict2 = dict(reader)

打印mydict2

以下是文本文件

  1,11 
2,22
3,33

以下是我运行测试时显示的结果代码:

  {1:11,2:22,3:33} 
11
{}

如前面两行结果输出所示,初始输出字典 mydict 被正确创建,并显示参考值。但是,第三行输出显示新字典 mydict2 的内容,应该从文本文件中读取数据。但是,文本文件内容以某种方式未读入新的字典,这是空的。



如何解决此问题将文本文件内容加载到字典? / p>

解决方案

问题是没有关闭你的文件。您的原始文件未完全刷新到磁盘。 与语句的将自动关闭文件,当块退出。

  import csv 

mydict = {1:11,2:22,3:33}
打印mydict
打印mydict [1]

with open('dict.csv','wb')as f:
writer = csv.writer(f)
为key,mydict中的值。 item():
writer.writerow([key,value])

with open('dict.csv','rb')as f:
reader = csv。读者(f)
mydict2 = dict(reader)

打印mydict2

输出:

  {1:11,2:22,3:33} 
11
{'1':'11','3':'33','2':'22'}


I'm a novice Python programmer so the answer may be simple. However, I've spent several days researching this single question and I'm stumped. This is also my first Stack Overflow post, so please be kind if I'm breaking some rule here. :)

Using Python, I want to write two fields in multiple rows to a new text file, and on a later run read that text file into a new dictionary to reference the previous values.

I am testing code that I found here: Example code. The solution provided has 64 up-votes and is marked as the best answer by the asker. So it seems to be correct for most people.

My test code succeeds in creating an initial dictionary mydict and saving it to a text file. However, when I attempt to read the text file back into a new dictionary mydict2, I get...nothing...nothing at all, no error message or anything, no clue, just nothing.

What am I missing here?

Here is my test code:

import csv

mydict = {1:11,2:22,3:33}
print mydict
print mydict[1]

writer = csv.writer(open('dict.csv', 'wb'))
for key, value in mydict.items():
   writer.writerow([key, value])

reader = csv.reader(open('dict.csv', 'rb'))
mydict2 = dict(reader)

print mydict2

Here are the contents of the text file that it creates:

1,11
2,22
3,33

Here are the results displayed when I run the test code:

{1: 11, 2: 22, 3: 33}
11
{}

As you can see in the first two lines of results output, the initial output dictionary, mydict, is created correctly and displays a referenced value. However, the third line of output displays the contents of the new dictionary, mydict2, which should have read the data from the text file. However, the text file contents were somehow not read to the new dictionary, which is empty.

How can I fix this to load the text file contents into the dictionary?

解决方案

The problem is not closing your files. Your original file was not completely flushed to disk. The with statement will automatically close files when the with block exits.

import csv

mydict = {1:11,2:22,3:33}
print mydict
print mydict[1]

with open('dict.csv','wb') as f:
    writer = csv.writer(f)
    for key, value in mydict.items():
        writer.writerow([key, value])

with open('dict.csv','rb') as f:
    reader = csv.reader(f)
    mydict2 = dict(reader)

print mydict2

Output:

{1: 11, 2: 22, 3: 33}
11
{'1': '11', '3': '33', '2': '22'}

这篇关于Python:将字典写入文本文件(CSV?),并将该文本文件读入字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 15:03