Python:读取文件并从不同的行向字典添加键和值 [英] Python: Reading a file and adding keys and values to dictionaries from different lines

查看:277
本文介绍了Python:读取文件并从不同的行向字典添加键和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Python还是很陌生,在处理基本上像这样的作业时遇到了麻烦:


#逐行读取WARC文件以识别string1。 / p>

#找到string1后,将字符串的一部分添加为字典的键。


#然后继续读取文件以识别string2,并添加一部分字符串2作为上一个键的值。


#继续浏览文件并进行同样的操作以构建字典。


我无法导入任何东西,这给我带来麻烦,尤其是添加键,然后将值保留为空,然后继续遍历文件以查找要用作值的string2。


我已经开始思考诸如将键保存到中间变量之类的东西,然后继续确定值,添加到中间变量,最后构建字典。

  def main():
###打开文件
file = open( warc_file.warc, rb)
filetxt = file.read()。decode( '如cii','ignore')
filedata = filetxt.split( \r\n)
词典= dict()
而文件数据中的行:
用于文件数据中的一行:
如果 WARC类型:响应在行中:
在文件数据中以
换行:
如果 WARC-Target-URI:内联:
urlkey = line.strip( WARC-Target-URI:)


解决方案

您的想法是将键存储为中间值。


我还建议使用以下代码段对行进行迭代。

 ,其中open(filename, rb)作为文件:
lines = file.readlines( )
换行:
print(line)

要在Python中创建字典条目,可以使用 dict.update()方法。
它允许您创建新键或更新键(如果键已存在)。

  d = dict ()#创建空字典
d.update({'key:None})#创建没有值的条目
d.update({'key:123})#更新值


I'm very new to Python and I'm having trouble working on an assignment which basically is like this:

#Read line by line a WARC file to identify string1.

#When string1 found, add part of the string as a key to a dictionary.

#Then continue reading file to identify string2, and add part of string2 as a value to the previous key.

#Keep going through file and doing the same to build the dictionary.

I can't import anything so it's causing me a bit of trouble, especially adding the key, then leaving the value empty and continue going through the file to find string2 to be used as value.

I've started thinking something like saving the key to an intermediate variable, then going on to identify the value, add to an intermediate variable and finally build the dictionary.

def main ():
###open the file
file = open("warc_file.warc", "rb")
filetxt = file.read().decode('ascii','ignore')
filedata = filetxt.split("\r\n")
dictionary = dict()
while line in filedata:
    for line in filedata:
        if "WARC-Type: response" in line:
            break
    for line in filedata:
        if "WARC-Target-URI: " in line:
           urlkey = line.strip("WARC-Target-URI: ")

解决方案

Your idea with storing the key to an intermediate value is good.

I also suggest using the following snippet to iterate over the lines.

with open(filename, "rb") as file:
    lines = file.readlines()
    for line in lines: 
        print(line)

To create dictionary entries in Python, the dict.update() method can be used. It allows you to create new keys or update values if the key already exists.

d = dict() # create empty dict
d.update({"key" : None}) # create entry without value
d.update({"key" : 123}) # update the value

这篇关于Python:读取文件并从不同的行向字典添加键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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