我有一个包含以下数据的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中执行此操作的最佳方法是什么?
答案 0 :(得分:0)
这是一种方法:
从文本文件中读取的示例代码:
#!/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)
这两个例子都使用列表推导。
要详细了解它们的工作原理,请阅读以下内容:
答案 1 :(得分:0)
你去了:
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