[英]How can I take a text file and create a triple nested list from it with tkinter python
I'm making a program that allows the user to log loot they receive from monsters in an MMO. 我正在制作一个程序,允许用户记录从MMO中的怪物那里收到的战利品。 I have the drop tables for each monster stored in text files. 我将每个怪物的放置表存储在文本文件中。 I've tried a few different formats but I still can't pin down exactly how to take that information into python and store it into a list of lists of lists. 我尝试了几种不同的格式,但是我仍然无法确切确定如何将这些信息带入python并将其存储到列表列表中。
The text file is formatted like this 文本文件的格式如下
item 1*4,5,8*ns
item 2*3*s
item 3*90,34*ns
The item # is the name of the item, the numbers are different quantities that can be dropped, and the s/ns is whether the item is stackable or not stackable in game. 物品编号是物品的名称,数字是可以丢弃的不同数量,而s / ns是物品在游戏中是否可堆叠。
I want the entire drop table of the monster to be stored in a list called currentDropTable so that I can reference the names and quantities of the items to pull photos and log the quantities dropped and stuff. 我希望将怪物的整个掉落表存储在一个名为currentDropTable的列表中,以便我可以引用这些物品的名称和数量来提取照片并记录掉入的物品数量。
The list for the above example should look like this 以上示例的列表应如下所示
[["item 1", ["4","5","8"], "ns"], ["item 2", ["2","3"], "s"], ["item 3", ["90","34"], "ns"]]
That way, I can reference currentDropTable[0][0] to get the name of an item, or if I want to log a drop of 4 of item 1, I can use currentDropTable[0][1][0]. 这样,我可以引用currentDropTable [0] [0]来获取项目的名称,或者,如果我想记录掉项目1的4,则可以使用currentDropTable [0] [1] [0]。
I hope this makes sense, I've tried the following and it almost works, but I don't know what to add or change to get the result I want. 我希望这是有道理的,我已经尝试了以下方法并且几乎可以正常工作,但是我不知道添加或更改要获得所需结果的内容。
def convert_drop_table(list):
global currentDropTable
currentDropTable = []
for i in list:
item = i.split('*')
currentDropTable.append(item)
dropTableFile = open("droptable.txt", "r").read().split('\n')
convert_drop_table(dropTableFile)
print(currentDropTable)
This prints everything properly except the quantities are still an entity without being a list, so it would look like 这样可以正确打印所有内容,但数量仍然是没有列表的实体,因此看起来像
[['item 1', '4,5,8', 'ns'], ['item 2', '2,3', 's']...etc]
I've tried nesting another for j in i, split(',') but then that breaks up everything, not just the list of quantities. 我试过在i中为j嵌套另一个,split(','),但这会破坏所有内容,而不仅仅是数量列表。
I hope I was clear, if I need to clarify anything let me know. 我希望我很清楚,如果我需要澄清任何事情,请告诉我。 This is the first time I've posted on here, usually I can just find another solution from the past but I haven't been able to find anyone who is trying to do or doing what I want to do. 这是我第一次在这里发布信息,通常我只能找到过去的另一种解决方案,但是我一直找不到能够尝试做的事情或做我想做的事情的人。
Thank you. 谢谢。
You want to split only the second entity by ','
so you don't need another loop. 您只想用','
分割第二个实体','
因此您不需要另一个循环。 Since you know that item = i.split('*')
returns a list of 3 items, you can simply change your innermost for-loop as follows, 由于您知道item = i.split('*')
返回3个项目的列表,因此您可以按如下所示更改最里面的for循环,
for i in list:
item = i.split('*')
item[1] = item[1].split(',')
currentDropTable.append(item)
Here you replace the second element of item
with a list of the quantities. 在这里,您用数量列表替换了item
的第二个元素。
You only need to split second element from that list. 您只需要从该列表中拆分第二个元素。
def convert_drop_table(list):
global currentDropTable
currentDropTable = []
for i in list:
item = i.split('*')
item[1] = item[1].split(',')
currentDropTable.append(item)
The first thing I feel bound to say is that it's usually a good idea to avoid using global
variables in any language. 我不得不说的第一件事是,避免使用任何语言的global
变量通常是一个好主意。 Errors involving them can be hard to track down. 涉及它们的错误可能很难找到。 In fact you could simply omit that function convert_drop_table
from your code and do what you need in-line. 实际上,您可以简单地从代码中省略该函数convert_drop_table
并直接执行您需要的操作。 Then readers aren't obliged to look elsewhere to find out what it does. 这样,读者就没有义务去其他地方了解它的作用。
And here's yet another way to parse those lines! 这是解析这些行的另一种方法 ! :) Look for the asterisks then use their positions to select what you want. :)查找星号,然后使用其位置选择所需的内容。
currentDropTable = []
with open('droptable.txt') as droptable:
for line in droptable:
line = line.strip()
p = line.find('*')
q = line.rfind('*')
currentDropTable.append([line[0:p], line[1+p:q], line[1+q:]])
print (currentDropTable)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.