python中如何取出{'cg_13': ['0', '5', '7', '8', '6', '4', '9'], 'cg_15': ['0', '5', '7', '4', '2', '1', '9']}中的cg_13
时间: 2023-12-21 13:27:12 浏览: 64
要取出字典中的键值对,可以使用字典的索引操作符`[]`。在这个例子中,可以通过`{'cg_13': ['0', '5', '7', '8', '6', '4', '9'], 'cg_15': ['0', '5', '7', '4', '2', '1', '9']}`['cg_13']来取出'cg_13'对应的值。具体代码如下:
```python
data = {'cg_13': ['0', '5', '7', '8', '6', '4', '9'], 'cg_15': ['0', '5', '7', '4', '2', '1', '9']}
result = data['cg_13']
print(result)
```
输出结果:
```
['0', '5', '7', '8', '6', '4', '9']
```
相关问题
python 如何取出字典的引号 {'cg_13': ['0', '5', '7', '8', '6', '4', '9'], 'cg_15': ['0', '5', '7', '4', '2', '1', '9']}
要取出字典中的引号,可以使用json.dumps()函数将字典转换为字符串,并设置参数ensure_ascii=False,这样就可以保留引号。
示例代码如下:
```python
import json
my_dict = {'cg_13': ['0', '5', '7', '8', '6', '4', '9'], 'cg_15': ['0', '5', '7', '4', '2', '1', '9']}
string_dict = json.dumps(my_dict, ensure_ascii=False)
print(string_dict)
```
输出结果:
```
{"cg_13": ["0", "5", "7", "8", "6", "4", "9"], "cg_15": ["0", "5", "7", "4", "2", "1", "9"]}
```
"" base64编解码处理 用于处理某些被加密的敏感词库 author : @h-j-13 time : 2018-7-18 """ import os import base64 def decode64file(path_file): """解码base64加密的文件""" with open(path_file, 'rb') as f: str_set = set() for line in f: s = line.strip() # strip()方法消除多余的空格之类的特殊字符 if s.endswith(b'Cg=='): s = s.replace(b'Cg==', b'') str_set.add(base64.b64decode(s)) return str_set train_data_url = r"C:\Users\曹福滨\Downloads\tc-corpus-\answer" def get_all_file_by_path(path=train_data_url): """获取某个目录下的所有训练文件""" file_path = [] dir_list = os.listdir(train_data_url) for d in dir_list: file_path.extend(map(lambda x: train_data_url + d + '/' + x, os.listdir(train_data_url + d))) return file_path def decode_file2utf8(file_path): """将文件从GB2312编码解码为utf8文件""" decode_error = False file_data = [] with open(file_path, 'r') as f: for l in f.readlines(): try: tmp = l.encode('gbk').decode('utf8') except Exception as e: decode_error = True tmp = '' file_data.append(tmp) if decode_error: os.remove(file_path) else: with open(file_path, 'w') as f: f.writelines(file_data) if __name__ == '__main__': for p in get_all_file_by_path(): decode_file2utf8(p)
这段代码是用来解码base64加密的文件,主要是用于处理一些被加密的敏感词库。作者是@h-j-13,时间是2018年7月18日。代码中使用了Python的os和base64模块,其中decode64file函数接受一个文件路径作为参数,读取文件内容并解码,最终返回一个字符串集合。
阅读全文