如何将文件读入字典 [英] How to read in a file into a dictionary

查看:143
本文介绍了如何将文件读入字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我需要做的是在文件clues.txt中阅读并存储到一个字典...到目前为止,我已经把它存入一个列表,但我正在努力用一个字典做它。

So what I need to do is read in the file "clues.txt" and store it into a dictionary... So far I have got it to store into a list but am struggling to do it with a dictionary...

我将其读入列表的代码如下所示...

My code for reading it into a list is as shown...

def read_clues(clues):
#TRYS TO OPEN THE FILE "CLUES.TXT"    
    try:
        readclues = open("clues.txt","r")
        for line in readclues:
#SAME CODE AS  TASK ONE HOWEVER THIS TIME IT IS DOING IT FOR CLUES
            clues.append(line[:len(line)-1])

        for clue in clues:
            print(clue)
        readclues.close()
        return clues
#IF THE FILE CANNOT BE FOUND IT WILL PRINT "ERROR FINDING FILE"
    except:
        print("Error finding file")

那么什么在文件中的线索是下面显示的字母和符号对...

So what's in the file clues are the letter and symbol pairings shown below...

A#
M*
N%

我的程序的下一部分涉及字典。基本上它做的是替换word.txt文件(如下所示),并替换其内的线索配对...演示如下所示...

The next part of my program involves the dictionary. Basically what it does is replaces the words.txt file (shown below) and replace the clue pairings inside it... A demonstration is shown below...

txt作为列表读入,因为它应该是...

words.txt is read in as a list as it should be...

#+/084&"
#3*#%#+
8%203:
,1$&
!-*%
.#7&33&
#*#71%
&-&641'2
#))85
9&330*

clues.txt目前以列表形式阅读,但需要作为字典阅读...

clues.txt is read in as a list at the moment but needs to be read in as a dictionary...

A+/084&"
A3MA%A+
8%203:
,1$&
!-M%
.A7&33&
AMA71%
&-&641'2
A))85
9&330

然后在words.txt中替换了线索...

The clues have then been replaced in words.txt...

这个代码是...但是需要在阅读字典时更改...

The code for this is... But this needs to be changed when reading into a dictionary...

#SUBSTITUTE THE LETTERS WITH SYMBOLS FROM THE CLUES
def replace_symbols(clues, words):
#SPLITS THE CLUES SO THAT THEY CAN BE REPLACED WITH THE LETTER

    for clue in clues:
        letter = clue[0]
        symbol = clue[-1]

        for index in range(len(words)):
#LOOPS THROUGH THE LIST TO FIND AN INDEX VALUE
            words[index] = words[index].replace(symbol,letter)
#RETURNS THE NEW COPY BACK TO THE ORIGINAL LIST
    return words

我正在尝试但是收到错误的代码...

The code which I am trying but receiving errors from...

def read_clues(clues):
    d = {}
    with open("clues.txt") as f:
        for line in f:
            (key, val) = line[1], line[0]
            d[key] = val


def replace_symbols(clues, words):
    for word in range(len(words)):
        for key, value in d.items():
            words[word] = words[word].replace(key, value)


推荐答案

尝试这样,它将根据线索文件的模式工作发布:

Try this it will work according to the pattern of clues file you posted:

d = {}
with open("file.txt") as f:
    for line in f:
        (key, val) = line[1], line[0]
        d[key] = val

尝试读取字典键值对和替换符号:

Try this for reading dictionary key value pairs and replacing symbols:

for word in range(len(words)):
    for key, value in d.items():
        words[word] = words[word].replace(key, value)

编辑:

d = {}
def read_clues(clues):
    global d
    with open("hey.txt") as f:
        for line in f:
            (key, val) = line[1], line[0]
            d[key] = val


def replace_symbols(clues, words):
    global d
    for word in range(len(words)):
        for key, value in d.items():
            words[word] = words[word].replace(key, value)

代码在一个 .py 文件并运行它,它将工作。您正在做的是您正在尝试将局部变量 d 调用在其范围之外,这就是为什么您收到错误的原因我已经将 d 变量全局。

Just put this code in a .py file and run it, it will work. What you are doing is you are trying to call a local variable d outside of its scope that is why you were getting the error now I have made that d variable global.

#REPLACES LETTERS 
print("======== The clues have been replaced ===========")
replace_symbols(clues, words) 
for key, value in d.items():
    print key, value #This will print the symbols and letters

这篇关于如何将文件读入字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆