试图将字符串放入列中

Trying to place strings into columns

有 3 列,级别 1-3。读取一个文件,文件的每一行包含各种数据,包括它所属的级别,位于字符串的后面。

Sample lines from file being read:

thing_1 - level 1
thing_17 - level 3
thing_22 - level 2

我想将每个“事物”分配给它对应的列。我查看了 pandas,但似乎 DataFrame 列不起作用,因为传递的数据需要具有与列数相匹配的属性,在我的例子中,我需要 3 列,但每一部分的数据只有 1 个数据点。

我该如何解决这个问题?

期望的输出:

level 1     level 2    level 3

thing_1     thing_22   thing_17

编辑:

在查看建议时,我可以进一步完善我的问题。我最多有 3 列,文件中的行需要分配给 3 列之一。大多数解决方案似乎需要这样的东西:

data = [['Mary', 20], ['John', 57]]
columns = ['Name', 'Age']

这对我不起作用,因为有 3 列,每条数据只进入一个。

这里还有一个我一开始没注意到的问题。如果你的每个级别都有相同数量的东西,那么你可以构建一个 dictionary and then use it to supply the table's columns to PrettyTable:

from prettytable import PrettyTable

# Create an empty dictionary.
levels = {}
with open('data.txt') as f:
    for line in f:
        # Remove trailing \n and split into the parts we want.
        thing, level = line.rstrip('\n').split(' - ')
        
        # If this is is a new level, set it to a list containing its thing.
        if level not in levels:
            levels[level] = [thing]
        # Otherwise, add the new thing to the level's list.
        else:
            levels[level].append(thing)

# Create the table, and add each level as a column
table = PrettyTable()
for level, things in levels.items():
    table.add_column(level, things)

print(table)

对于您显示的示例数据,这将打印:

+---------+----------+----------+
| level 1 | level 3  | level 2  |
+---------+----------+----------+
| thing_1 | thing_17 | thing_22 |
+---------+----------+----------+

并发症

我可能不会发布答案(相信它在 this answer 中已经充分涵盖),除非我意识到这里有一个不直观的障碍。如果你的关卡包含不同数量的东西,你会得到这样的错误:

Exception: Column length 2 does not match number of rows 1!

因为 none 现成的解决方案对此有一个明显的“自动”解决方案,这里有一个简单的方法可以做到这一点。像以前一样构建字典,然后:

# Find the length of the longest list of things.
longest = max(len(things) for things in levels.values())

table = PrettyTable()
for level, things in levels.items():
    # Pad out the list if it's shorter than the longest.
    things += ['-'] * (longest - len(things))
    table.add_column(level, things)

print(table)

这将打印如下内容:

+---------+----------+----------+
| level 1 | level 3  | level 2  |
+---------+----------+----------+
| thing_1 | thing_17 | thing_22 |
|    -    |    -     | thing_5  |
+---------+----------+----------+

额外

如果所有这些都有意义,并且您想了解可以简化其中一部分的方法,请查看 Python 的 defaultdict。它可以处理“检查此键是否已存在”过程,如果不存在则提供默认值(在本例中为新列表)。

from collections import defaultdict

levels = defaultdict(list)
with open('data.txt') as f:
    for line in f:
        # Remove trailing \n and split into the parts we want.
        thing, level = line.rstrip('\n').split(' - ')
        
        # Automatically handles adding a new key if needed:
        levels[level].append(thing)