【问题标题】:padding data in python在python中填充数据
【发布时间】:2017-01-24 11:04:14
【问题描述】:

我有一个包含以下数据的 txt 文件:

43,70,90,12,98,54,23
80,100,21,67
23,45
30

我想对数据进行填充,以便数据具有相同的长度,例如,我想使用数字 0 填充,以便输出应该是:

43,70,90,12,98,54,23
80,100,21,67,0,0,0
23,45,0,0,0,0,0
30,0,0,0,0,0,0

在 Python 中执行此操作的最佳方法是什么?

【问题讨论】:

  • 向我们展示你到目前为止所做的尝试
  • 顶行是否总是最长且完整?
  • 最长的数据并不总是位于最上面一行,它们可能是随机的。

标签: python python-2.7 python-3.x numpy


【解决方案1】:

这是一种方法:

  1. 确定您的文本/文件的最大列数
  2. 在没有最大数量的行中附加额外的列

从文本文件中读取的示例代码:

#!/usr/bin/env python3

output = ""
with open("1.txt") as f:
    # Determine number of columns
    cols = max([len(line.split(",")) for line in f])
    print("Maximum number of columns: %d" % cols)

    # Build output string, starting from beginning of file
    f.seek(0)
    for line in f:
        output += line.strip() + ",0" * (cols - len(line.split(","))) + "\n"
print("Output:", output)

从字符串文字中读取的示例代码:

#!/usr/bin/env python3

# You could read text from file. Here we use a literal string
text = """43,70,90,12,98,54,23
80,100,21,67
23,45
30
"""

# Split text to lines
lines = text.splitlines(False)

# Determine number of columns
cols = max([len(line.split(",")) for line in lines])
print("Maximum number of columns: %d" % cols)

output = ""
for line in lines:
    output += line + ",0" * (cols - len(line.split(","))) + "\n"

print("Output:", output)

两个示例都使用列表推导。

要详细了解它们的工作原理,请阅读以下内容:

【讨论】:

    【解决方案2】:

    你去吧:

    import numpy as np
    import csv
    
    input_list = []  # list to keep track of input
    max_number_of_columns = 0  # maximum number of columns
    number_of_rows = 0
    
    with open("test.txt") as testfile:
        for line in testfile:
            # read line, strip end of line and split at the comma
            line = line.replace("\n", "") 
            values = line.split(",")
    
            number_of_rows += 1
            if len(values) > max_number_of_columns:
                max_number_of_columns = len(values)
            print values
            input_list.append(values)  # add new values to the list
    # initialize all zero numpy array
    np_array = np.zeros((number_of_rows, max_number_of_columns))
    for i, row in enumerate(input_list):
        np_array[i,:len(row)] = row  # write entries into the array
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多