用python编写空列

2024-09-25 16:24:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下两种类型的txt文件:

文件1

Sample1012, Male, 36, Stinky, Bad Hair
Sample1043, Female, 28, Hot, Short Hair, Hot Body, Hates Me
Sample23905, Female, 42, Cougar, Long Hair, Chub
Sample123, Male, 32, Party Guy

文件2

^{pr2}$

我只想编写一个简单的Python脚本来基于sample字段连接这些文件,但是数据列的随机数量一直存在问题。例如,我最后得出:

Sample1012, Male, 36, Stinky, Bad Hair, ALIVE, Sample1012, Alone
Sample1043, Female, 28, Hot, Short Hair, Hot Body, Hates Me, DEAD, Sample1043, Too Hot, Exploded
Sample23905, Female, 42, Cougar, Long Hair, Chub, ALIVE, Sample23905, STD
Sample123, Male, 32, Party Guy, DEAD, Sample123, Car Accident, Drunk, Dumb

当我想要的是:

Sample1012, Male, 36, Stinky, Bad Hair, EMPTY COLUMN, EMPTY COLUMN, ALIVE, Sample1012, Alone
Sample1043, Female, 28, Hot, Short Hair, Hot Body, Hates Me, DEAD, Sample1043, Too Hot, Exploded
Sample23905, Female, 42, Cougar, Long Hair, Chub, EMPTY COLUMN, ALIVE, Sample23905, STD
Sample123, Male, 32, Party Guy, EMPTY COLUMN, EMPTY COLUMN, EMPTY COLUMN, DEAD, Sample123, Car Accident, Drunk, Dumb

基本上,我只是用.readlines()读取两个文件,然后用简单的“==”将相关列与示例ID进行比较,如果为真,那么它将打印出第一个文件和第二个文件中的行。在

不知道如何使用len()来确定file1中的最大列数,以便在从另一个文件追加行之前,如果不是max number of columns,那么我可以在每一行末尾说明这个值(前提是“==”为真)。在

非常感谢任何帮助。在

更新:

我现在得到的是:

import sys
import csv

usage = "usage: python Integrator.py <table_file> <project_file> <outfile>"
if len(sys.argv) != 4:
    print usage
    sys.exit(0)

project = open(sys.argv[1], "rb")
table = open(sys.argv[2], "rb").readlines()
outfile = open(sys.argv[3], "w")

table[0] = "Total Table Output \n"

newtablefile = open(sys.argv[2], "w")
for line in table:
    newtablefile.write(line)

projectfile = csv.reader(project, delimiter="\t")
newtablefile = csv.reader(table, delimiter="\t")

result = []

for p in projectfile:
    print p
    for t in newtablefile:
        #print t
        if p[1].strip() == t[0].strip():
            del t[0]
            load = p + t
            result.append(load)


for line in result:
    outfile.write(line)

outfile.close()

不能让for循环一起工作-不要介意在车站的愚蠢的东西。第一个文件中有一个空白行。在


Tags: 文件forsystablecolumnmalefemaleempty
3条回答
with open('file1') as f1, open('file2') as f2:
    dic = {}
    #Store the data from file2 in a dictionary, with second column as key
    for line in f2:
        data = line.strip().split(', ')
        key = data[1]
        dic[key] = data
    #now iterate over each line in file1
    for line in f1:
        data = line.strip().split(', ')
        #number of empty columns = `(7-len(data))`
        data = data + ['EMPTY COLUMN']*(7-len(data))
        print '{}, {}'.format(", ".join(data), ', '.join(dic[data[0]]))

输出:

^{pr2}$

嗯,为了提高效率,你应该使用rdbms,但是你可以使用字典来做得更好。在

当您在第一个逗号上使用readline()时,只需在第一个逗号之前拆分所有内容,并将其用作键,值就是列表。在

所以有点像

{'Sample1012': ['Sample1012', 'Male', 36, 'Stinky', 'Bad Hair']}

现在你可以对另一个文件做同样的事情

简单地说

^{pr2}$

然后把所有相应的东西都加到第一个字典里。。在

这只会让你的生活更轻松

不知道“空列”是从哪里来的建议输出。。。如果列应该与定义的模式匹配,那么输入文件中必须有空白点。否则,这将起作用。。。在

import csv


f1 = open("test1.txt", 'rb')
reader1 = csv.reader(f1)
f2 = open("test2.txt", 'rb')
reader2 = csv.reader(f2)
result = []

for entry in reader1:
    print entry
    for row in reader2:
        print row
        if entry[0].strip() == row[1].strip():
            del row[1]
            load = entry + row
            result.append(load)

for line in result:
    print line

编辑-

如果你需要跳过其中一个文件中的一行,你可以这样做 reader1.next() 它将指针移到下一行输入。在

您的例子创建了一个输出文件,向其写入数据,然后尝试读取它,而不必关闭文件并重新打开它,或者以可读写的方式打开它。。。我不能发誓,但我想那可能是你的问题。幸运的是,您不需要使用.next()方法完成所有这些操作。在

相关问题 更多 >