首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获得具有类列表数据文件中所有列的行的确切值

如何获得具有类列表数据文件中所有列的行的确切值
EN

Stack Overflow用户
提问于 2022-01-23 11:29:01
回答 1查看 157关注 0票数 0

试验数据

我想做的是,搜索行的“名称”数据,找到它,并提取同一行上的所有数据。

例如,[{"name": "Rusted Abyss Scarab", "exaltedValue": 0, "chaosValue": 0.5, "currType": "Chaos Orb"}]考虑到这一行,我想在我的函数中搜索"Abyss“,当我在搜索中得到它时,我想提取名称、exaltedValue、chaosValue和currType。

代码语言:javascript
复制
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

当我运行具有上述搜索功能的完整代码

代码语言:javascript
复制
[c1.name for c1 in ScarabValues.exportfunction.search("Abyss")] #c = ScarabValues, c.Scarab_Values

 ['Rusted Abyss Scarab']

第一行,它输出第二行。我想完整地打印像“锈迹斑斑”,"0","0.5",“混沌球”这样的值。多么?

EN

回答 1

Stack Overflow用户

发布于 2022-01-23 12:02:26

您应该首先尝试转换为字典对象,然后运行查找。

代码语言:javascript
复制
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

这将基本上检查所有元素中的名称,并在字典中打印所有值。如果愿意,可以更改输出格式。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70821575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档