Python 附加具有不同变量的文件
我有以下格式的3个文件- 文件1:Python 附加具有不同变量的文件,python,Python,我有以下格式的3个文件- 文件1: ID Var1 Var2 001 5 10 002 12 6 文件2: ID Var1 Var3 Var5 003 5 10 9 004 12 6 1 文件3: ID Var3 Var4 005 5 10 006 12 6 我想要以下格式的输出 ID Var1 Var2 Var3 Var4 Var5 001 5 10 0 0 0 002 12 6 0 0 0 003 5 0 10 0 9 004 12 0 6 0 1 005 0 0 5 10 0 006
ID Var1 Var2
001 5 10
002 12 6
文件2:
ID Var1 Var3 Var5
003 5 10 9
004 12 6 1
文件3:
ID Var3 Var4
005 5 10
006 12 6
我想要以下格式的输出
ID Var1 Var2 Var3 Var4 Var5
001 5 10 0 0 0
002 12 6 0 0 0
003 5 0 10 0 9
004 12 0 6 0 1
005 0 0 5 10 0
006 0 0 12 6 0
请让我知道如何在python中做到这一点,如前所述,您应该看看csv模块,下面是一些帮助您入门的内容
outfile = open("output.txt", 'w')
for file_ in os.listdir("\path\to\my\files"):
with open(file_) as f:
for line_number, line in enumerate(file_):
if line_number > 0: #omit the headers
outfile.write(line)
使用python操作文件似乎也是一个挑战,也许您可以搜索其中一些文件,看看其他人是如何操作的
#use fileinput module if you're reading multiple files at once
import fileinput
dic = {} # initialize an empty dict. This swill be used to store the value of
# (id,var) pair fetched from the file.
for line in fileinput.input(['file1','file2','file3']):
#if 'ID' is present in the line then it means it is the header line
if 'ID' in line:
vars = line.split()[1:] # extract the vars from it
# for file1 vars would be ['Var1', 'Var2']
else: #else it is normal line
spl =line.split() # split the line at whitespaces
# for the line '001 5 10\n' this would return
# ['001', '5', '10']
idx, vals = spl[0], spl[1:] # assign the first value from spl
# to idx and rest to vals
#now use zip to iterate over vars and vals, zip will return
#item on the same index from the iterables passed to it.
for x, y in zip(vars, vals):
dic[idx,x] = y # use a tuple ('001','Var1') as key and
# assign the value '5' to it. Similarly
# ('001','Var2') will be assigned '10'
#get a sorted list of unique vars and Ids
vars = sorted(set(item[1] for item in dic))
idxs = sorted(set(item[0] for item in dic), key = int)
print " ".join(vars) #print header
# now iterate over the IDs and for each ID print the pick var from Vars and print the
# value of (id,Var),,, etc.
for x in idxs:
# dict.get will return the default value '0' if a
# combination of (id,var) is not found in dict.
print x," ".join(dic.get((x,y),'0') for y in vars)
#use string formatting for better looking output.
输出:
Var1 Var2 Var3 Var4 Var5
001 5 10 0 0 0
002 12 6 0 0 0
003 5 0 10 0 9
004 12 0 6 0 1
005 0 0 5 10 0
006 0 0 12 6 0
要合并多个文件,可以使用类似这样的函数,利用Python的
defaultdict
:
def read_from_file(filename, dictionary):
with open(filename) as f:
lines = f.read().splitlines()
head, body = lines[0].split(), lines[1:]
for line in body:
for i, item in enumerate(line.split()):
if i == 0:
d = dictionary[item]
else:
d[head[i]] = item
from collections import defaultdict
from pprint import pprint
d = defaultdict(defaultdict)
read_from_file("file1", d)
read_from_file("file2", d)
read_from_file("file3", d)
pprint(dict(d))
输出:
{'001': defaultdict(None, {'Var1': '5', 'Var2': '10'}),
'002': defaultdict(None, {'Var1': '12', 'Var2': '6'}),
'003': defaultdict(None, {'Var5': '9', 'Var1': '5', 'Var3': '10'}),
'004': defaultdict(None, {'Var5': '1', 'Var1': '12', 'Var3': '6'}),
'005': defaultdict(None, {'Var4': '10', 'Var3': '5'}),
'006': defaultdict(None, {'Var4': '6', 'Var3': '12'})}
现在所要做的就是将这本字典作为一个表漂亮地打印出来。欢迎使用堆栈溢出!看起来你想让我们为你写些代码。虽然许多用户愿意为陷入困境的程序员编写代码,但他们通常只在海报已经试图自己解决问题时才提供帮助。演示这项工作的一个好方法是,包括您迄今为止编写的代码、示例输入(如果有)、预期输出和实际获得的输出(控制台输出、堆栈跟踪、编译器错误——任何适用的)。您提供的详细信息越多,您可能收到的答案就越多。这些空格是在您的文件或选项卡中的值之间吗?你看过报纸了吗?它或多或少可以开箱即用。@TimPietzcker-这些是@MartijnPieters文件中的空格-我是python新手,因此获得有用函数的任何帮助都将非常有用appreciated@abhishekraghuvanshi当前位置如果你是新来的,我会从经历开始。