【问题标题】:Trying to place strings into columns试图将字符串放入列中
【发布时间】:2021-02-15 19:42:20
【问题描述】:

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

Sample lines from file being read:

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

我想将每个“事物”分配给它对应的列。我研究过熊猫,但似乎 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 列,每条数据只进入一个。

【问题讨论】:

  • 一个或多个级别是否有可能每个级别都有不止一件事?
  • 每列将包含多个内容,但每一行只有一个级别。
  • 你可以使用 Pandas,但我认为你可能最好看看更轻量级的输出包,比如这里提到的一个:stackoverflow.com/a/26937531/12975140
  • 谢谢,我会尝试根据这些提出解决方案。我将不得不跳出框框思考,因为这些解决方案仍然要求每条数据都与列数相关,在我的情况下,我有一定数量的列,其中的数据需要分类到适当的列中。
  • 我没有看过所有这些,但是 PrettyTable 和 Tabulate(可能还有其他)不需要您逐行提供数据;您可以改为逐列提供它。我建议将文本文件解析为dictionary,其中每个键是一个级别,其值是一个事物列表。然后你可以将它传递给你喜欢的输出方法。 (Pandas 也可以让你这样做,但是对于像这样你只是在进行输出格式化的事情来说,这可能是矫枉过正。)

标签: python output


【解决方案1】:

这里还有一个我一开始没有注意到的皱纹。如果您的每个级别的事物数量相同,那么您可以构建一个dictionary,然后使用它将表格的列提供给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!

因为现成的解决方案都没有明显的“自动”解决方案,所以这里有一个简单的方法。像以前一样构建字典,然后:

# 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)

【讨论】:

  • 非常感谢!我目前正在努力将其集成到我的代码中......将报告回来
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-15
  • 2016-01-02
  • 2020-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多