如何在类似列表的数据文件中获取包含所有列的行的精确值
How to get exact values of a line with all columns in a list-like data file
我想做的是,搜索行的“名称”数据,找到它,然后提取同一行上的所有数据。
例如,[{"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
当我 运行 full code with the search function above 和
[c1.name for c1 in ScarabValues.exportfunction.search("Abyss")] #c = ScarabValues, c.Scarab_Values
['Rusted Abyss Scarab']
第一行,输出第二行。我想完整打印 ["Rusted Abyss Scarab"]、["0"]、["0.5"]、["Chaos Orb"] 等值(它们都来自同一行)。怎么样?
您可以将数据解析为字典并使用它获取数据。
import json
item = """[{"name": "Winged Abyss Scarab", "exaltedValue": 0, "chaosValue": 90.0, "currType": "Chaos Orb"}]"""
# Convert it to dict object, indexing is used since it is a list with a single element
parsed = json.loads(item)[0]
# Now you can access to the data with keys
name = parsed['name'] # 'Winged Abyss Scarab'
exalted_value = parsed['exaltedValue'] # 0
chaos_value = parsed['chaosValue'] # 90.0
curr_type = parsed['currType'] # 'Chaos Orb'
当然,没有 json 模块也是可行的,但这会使事情变得过于复杂。
您应该首先尝试转换为字典对象,然后 运行 查找
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
这将基本上检查所有元素中的名称并打印字典中的所有值。如果需要,您可以更改输出格式。
我想做的是,搜索行的“名称”数据,找到它,然后提取同一行上的所有数据。
例如,[{"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
当我 运行 full code with the search function above 和
[c1.name for c1 in ScarabValues.exportfunction.search("Abyss")] #c = ScarabValues, c.Scarab_Values
['Rusted Abyss Scarab']
第一行,输出第二行。我想完整打印 ["Rusted Abyss Scarab"]、["0"]、["0.5"]、["Chaos Orb"] 等值(它们都来自同一行)。怎么样?
您可以将数据解析为字典并使用它获取数据。
import json
item = """[{"name": "Winged Abyss Scarab", "exaltedValue": 0, "chaosValue": 90.0, "currType": "Chaos Orb"}]"""
# Convert it to dict object, indexing is used since it is a list with a single element
parsed = json.loads(item)[0]
# Now you can access to the data with keys
name = parsed['name'] # 'Winged Abyss Scarab'
exalted_value = parsed['exaltedValue'] # 0
chaos_value = parsed['chaosValue'] # 90.0
curr_type = parsed['currType'] # 'Chaos Orb'
当然,没有 json 模块也是可行的,但这会使事情变得过于复杂。
您应该首先尝试转换为字典对象,然后 运行 查找
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
这将基本上检查所有元素中的名称并打印字典中的所有值。如果需要,您可以更改输出格式。