简体   繁体   English

从.txt Web文件中获取数据(作为键,值集)到字典中

[英]fetch data from .txt web file (as key,value set) into dictionary

I need to download content of .txt file from web, separate each key,value set and insert them into dictionary. 我需要从Web下载.txt文件的内容,分离每个键,值集并将它们插入字典中。

Here is txt file where are my parameters which have to be placed in dict-> http://mign.pl/ver.txt 这是txt文件,我的参数必须放在dict-> http://mign.pl/ver.txt中

and dict should looks like this: dic = {"Version": 1.0, "Hires": False, "Temperature": 55, "Description": "some description"} 并且dict应该看起来像这样: dic = {"Version": 1.0, "Hires": False, "Temperature": 55, "Description": "some description"}

I have already used urllib2 to download content: 我已经使用urllib2下载内容:

import urllib.request as urllib2 
url = urllib2.urlopen('http://www.mign.pl/ver.txt')

What next? 接下来是什么?

You can use built-in configparser module to parse the values ( doc ): 您可以使用内置的configparser模块来解析值( doc ):

import configparser
import urllib.request as urllib2

data = urllib2.urlopen('http://www.mign.pl/ver.txt')

c = configparser.ConfigParser()
c.read_string('[root]\n' + data.read().decode('utf-8'))
print({k:v for k, v in zip(c['root'], c['root'].values())})

Prints: 印刷品:

{'version': '1.0', 'hires': 'False', 'temperature': '55', 'description': 'Some description'}

Or just work with connfigparser directly (eg print(c['root']['version']) will print 1.0 ) 或者直接使用connfigparser(例如, print(c['root']['version'])将输出1.0

try this : 尝试这个 :

import urllib.request as urllib2
url = urllib2.urlopen('http://www.mign.pl/ver.txt')
d1={}
x=url.read().decode("utf-8")
d=x.split("\n")
for i in d:
    z=i.split("=")
    d1[str(z[0])]=z[1]
print(d1)

output: 输出:

{'Version ': ' 1.0', 'Hires ': ' False', 'Description ': ' Some description', 'Temperature ': ' 55'}

暂无
暂无

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

相关问题 如果给定的键和值从字典列表中匹配,则获取所有字典 - Fetch all dictionary if given key and value matches in from a list of dictionary 从带有关键年份和值列表的 txt 文件创建字典 - Creating a dictionary from a txt file with key year and values a list 读取txt文件数据作为字典 - Read txt file data as dictionary 从文本文件检查字典键值 - Checking Dictionary Key Value from Text File 将带有书目数据的字符串从.txt文件中提取到python中的字典中 - Extract string with bibliographic data from a .txt file into a dictionary in python 使用字典理解解析从文件到字典的键值 - Parsing a key, value from file into dictionary using dictionary comprehension 如何使用我想要获取的字典的一个键/值对从字典列表中访问字典 - How to access the dictionary from a list of dictionaries using one key/value pair of the dictionary that I want to fetch 从文本文件(Python 3)导入数据时,向预先存在的字典键添加值 - Adding a value to a pre existing dictionary key when importing the data from a text file (Python 3) 通过 Python 将 JSON 中的键值对提取到 TXT 文件中 - Extracting key value pairs from JSON into a TXT file via Python 将一个字典中的带有空列表的键与另一个包含该键作为值的字典合并后找到最小的集合 - Find the smallest set after merging key with empty list from one dictionary with another dictionary containing that key as value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM