python - 将文件导入 json 密钥
问题描述
我希望我的程序进入一个文件并获取所有行并将它们放入一个 json 键中。
编码:
def import_proxies():
global proxies_json
proxies_json = {'http': {}}
with open("proxies.txt", 'r', encoding='utf8') as proxy_file:
for line in proxy_file:
val = line.split()
proxies_json[['http'][val]] = val
print(proxies_json)
import_proxies()
My erorr: TypeError: list indices must be integers or slices, not list
我想要它做的是将文件行作为http
键中的值导入(proxies_json = {'http': {}}
解决方案
proxies_json 似乎是一本字典。所以这样的事情应该有效
def import_proxies():
global proxies_json
proxies_json = {'http': {}}
with open("proxies.txt", 'r', encoding='utf8') as proxy_file:
for line in proxy_file:
val = line.split()
data = proxies_json.get('http', {})
for item in val:
data[item] = item
proxies_json['http'] = data
print(proxies_json)
推荐阅读
- amazon-web-services - 从多个域提供 SPA
- r - parse_date_time 转换字符向量的问题
- vue.js - 谷歌云功能:未找到依赖关系
- javascript - How to get filter value for each effect?
- javascript - 尝试使用 jQuery 控制台记录一个 id
- python - 如果特定年份介于两个日期python之间,则返回值
- c# - 是否可以使 typescript-ref DTO 生成器尊重可为空的属性?
- android - ffmpeg 在 c++ 中将 AV_SAMPLE_FMT_S16 转换为 AV_SAMPLE_FMT_FLTP
- repast-simphony - 有没有一种简单的方法可以暂时关闭部分场景?
- javascript - 停止在其中添加文本