简体   繁体   English

在读取 txt 文件行时。 我不能“追加列表”或“更新字典”为什么?

[英]While reading txt file lines. I can't “append list” or “update dictionary” why?

I have txt file like that:我有这样的txt文件:

"aroint" : "Lorem.",
"agama" : "Simply.",
"allantoidea" : "Dummy.",
"ampelopsis" : "Whiske"\"red.",
"zopilote" : "Vulture.\n\n",
"zooedendrium" : "Infusoria."

I tried to read the txt file, convert Python dictionary and then create the json file我尝试读取txt文件,转换Python字典,然后创建json文件

import json
dictionary = {}
with open('/Users/stackoverflowlover/Desktop/source.txt', "r") as f:
    for line in f:
        s = (line.replace("\"", "").replace("\n\n", "").replace("\n", "").strip().split(":"))
        xkey = (s[0])
        xvalue = (s[-1])
        zvalue = str(xvalue)
        value = zvalue[:0] + zvalue[0 + 1:]
        key = xkey.replace(' ', '', 1)
        dict = {'key1': 'stackoverflow'}
        dictadd={key:value}
        (dict.update(dictadd))
        dictionary_list = []
        dictionary_list.append(key)
        dictionary_list.append(value)
        
print(dictionary_list)
with open("/Users/stackoverflowlover/Desktop/test.json", 'w', encoding='utf8') as f3:
  json.dump(dict, f3, ensure_ascii=False, indent=1)
print(json.dumps(dict, sort_keys=True, indent=4))

My output:我的 output:

['zooedendrium', 'Infusoria.']
{
    "key1": "stackoverflow",
    "zooedendrium": "Infusoria."
}

When I try to read lines I can see all of them, but after I can see just the last lines dictionary.当我尝试阅读行时,我可以看到所有行,但是在我只能看到最后一行字典之后。 How I can fix that?我该如何解决?

Here you go.这里是 go。 below logic will read you file and convert it to the JSON structure which you are looking as output下面的逻辑将读取您的文件并将其转换为您正在查看的 JSON 结构 output

import re
import json

f = open("/Users/stackoverflowlover/Desktop/source.txt", "r")
data = {}

for line in f:
        line = line.split(":")
        line[0] = re.sub("[^\w]+", "", line[0])
        line[1] = re.sub("[^\w]+", "", line[1])
        data[line[0]] = line[1]
        
print(data)

暂无
暂无

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

相关问题 如何通过读取.txt文件的每一行来将键值添加到字典中并更新值? - How can I add key values into a dictionary from reading in each line of a .txt file and update values? 从 txt 文件中读取行并创建一个字典,其中值是元组列表 - Reading lines from a txt file and create a dictionary where values are list of tuples 将txt文件读入字典 - Reading a txt file into a dictionary 我有一个包含几行的.txt 文件。 我必须从中删除所有带有字母“a”的行并将其写入另一个文件。 我该怎么做? - I've got a .txt file with a few lines. I have to remove all the lines with the letter "a" from it and write it to another file. How do I do it? 为什么不能将字符串对象附加到字典 - Why can't I append a string object to a dictionary 为什么不能从字典中追加此值? - why can't I append this value from a dictionary? 当使用urlparser在日志文件中循环访问某些行时缺少特定参数时,如何在列表中附加null - How can I append null in list when particular parameter is absent for some lines while iterating through log file using urlparser 结合每两行,同时在python中读取.txt文件 - Combine every two lines while reading .txt file in python 使用 reader + itertools 创建带有嵌套列表的字典(从 txt 文件读取) - Creating a dictionary with nested list with reader + itertools (reading from txt file) 如何将文件行从文件追加到两个特定行之间的列表中? - How can I append lines of text from a file to a list in between two specific lines?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM