我想做的是,搜索行的“名称”数据,找到它,并提取同一行上的所有数据。
例如,[{"name": "Rusted Abyss Scarab", "exaltedValue": 0, "chaosValue": 0.5, "currType": "Chaos Orb"}]考虑到这一行,我想在我的函数中搜索"Abyss“,当我在搜索中得到它时,我想提取名称、exaltedValue、chaosValue和currType。
class SearchScarab(list):
def search(self,name):
matching_items = []
for item in self:
if name in item.name: #Matching name column here and it is success.
matching_items.append(item) #Appending the item name to the matching_item
os.chdir(r"C:\Users\emosc\PycharmProjects\GithubPushs\psychescape_price_fetcher\psychescape_price_fetcher\values")
with open("scarab.txt", "r") as file:
data = file.read()
index = matching_items.index(item)
print(index) #Trying to get index of the item line
print(data[index]) #Print data of the index of item line, prints nothing.
break
return matching_items当我运行具有上述搜索功能的完整代码时
[c1.name for c1 in ScarabValues.exportfunction.search("Abyss")] #c = ScarabValues, c.Scarab_Values
['Rusted Abyss Scarab']第一行,它输出第二行。我想完整地打印像“锈迹斑斑”,"0","0.5",“混沌球”这样的值。多么?
发布于 2022-01-23 12:02:26
您应该首先尝试转换为字典对象,然后运行查找。
import json
data = []
def find_name(value):
matches = []
for item in data:
if value in item['name']:
matches += [item]
for match in matches:
for val in match.values():
print(val, end=", ")
print()
with open('<path to the file>', 'r') as f:
for line in f.readlines():
data += json.loads(line.strip())
find_name("Abyss")
>>> Winged Abyss Scarab, 0, 90.0, Chaos Orb
... Gilded Abyss Scarab, 0, 30.0, Chaos Orb
... Polished Abyss Scarab, 0, 3.0, Chaos Orb
... Rusted Abyss Scarab, 0, 0.5, Chaos Orb这将基本上检查所有元素中的名称,并在字典中打印所有值。如果愿意,可以更改输出格式。
https://stackoverflow.com/questions/70821575
复制相似问题