简体   繁体   English

如何在 python 的关键字之后打印?

[英]how to print after the keyword from python?

i have following string in python我在 python 中有以下字符串

b'{"personId":"65a83de6-b512-4410-81d2-ada57f18112a","persistedFaceIds":["792b31df-403f-4378-911b-8c06c06be8fa"],"name":"waqas"}'

I want to print the all alphabet next to keyword "name" such that my output should be我想打印关键字“名称”旁边的所有字母,这样我的 output 应该是

waqas 

Note the waqas can be changed to any number so i want print any name next to keyword name using string operation or regex?请注意,waqas 可以更改为任何数字,所以我想使用字符串操作或正则表达式在关键字名称旁边打印任何名称?

First you need to decode the string since it is binary b .首先,您需要解码字符串,因为它是二进制b Then use literal eval to make the dictionary, then you can access by key然后使用literal eval制作字典,然后就可以key访问了

>>> s = b'{"personId":"65a83de6-b512-4410-81d2-ada57f18112a","persistedFaceIds":["792b31df-403f-4378-911b-8c06c06be8fa"],"name":"waqas"}'
>>> import ast
>>> ast.literal_eval(s.decode())['name']
'waqas'

It is likely you should be reading your data into your program in a different manner than you are doing now.您可能应该以与现在不同的方式将数据读入程序。

If I assume your data is inside a JSON file, try something like the following, using the built-in json module:如果我假设您的数据在 JSON 文件中,请使用内置的json模块尝试以下操作:

import json

with open(filename) as fp:
    data = json.load(fp)    
print(data['name'])

You can convert the string into a dictionary with json.loads()您可以使用json.loads()将字符串转换为字典

import json
mystring = b'{"personId":"65a83de6-b512-4410-81d2-ada57f18112a","persistedFaceIds":["792b31df-403f-4378-911b-8c06c06be8fa"],"name":"waqas"}'
mydict = json.loads(mystring)
print(mydict["name"])
# output 'waqas'

if you want a more algorithmic way to extract the value of name :如果您想要一种更算法的方法来提取name的值:

s = b'{"personId":"65a83de6-b512-4410-81d2-ada57f18112a",\
"persistedFaceIds":["792b31df-403f-4378-911b-8c06c06be8fa"],\
"name":"waqas"}'

s = s.decode("utf-8")

key = '"name":"'
start = s.find(key) + len(key)
stop = s.find('"', start + 1)

extracted_string = s[start : stop]
print(extracted_string)

output output

waqas

First you need to convert the string into a proper JSON Format by removing b from the string using substring in python suppose you have a variable x :首先,您需要使用 python 中的 substring 从字符串中删除 b ,将字符串转换为正确的JSON格式,假设您有一个变量x

import json
x = x[1:];
dict = json.loads(x) //convert JSON string into dictionary
print(dict["name"])

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

相关问题 如何从文本文件中打印包括关键字和关键字之后的文本? - how to print the text including keyword and after keyword from a text file? 在关键字python之后打印字符串 - print string after keyword python 如何在文件中从and到关键字中搜索特定词并在python中打印句子 - how to search for a specific from and to keyword in a file and print the sentence in python Python,从关键字打印到下一个点 - Python, print from keyword to next dot 如何完全禁止python 2.7中的print关键字? - How to completely ban print keyword in python 2.7? 如何在 python 中的关键字之后从字符串中提取浮点数 - How to extract a float from a string after a keyword in python 如何让 python 查找特定关键字并从主要列表中打印出包含该关键字的特定列表。 (二维列表) - How to let python to look for a specific keyword and print out that particular list that includes that keyword from a major list. (2D list) Python 使用标题或关键字从文本文件中打印段落 - Python use title or keyword to print paragraph from text file 如何使用 python 在关键字后搜索 lxml - How to Search lxml after a keyword using python 如何搜索关键字并打印文件,只打印关键字中的行 - How do I search a keyword and print a file with only print the lines from the keyword on
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM