如何在python脚本中使用Postgres将单值字段转换为多值字段?

如何在python脚本中使用Postgres将单值字段转换为多值字段?,python,postgresql,Python,Postgresql,我目前正在为一个bioinfomatics类处理数据库,我在格式化SQL输出时遇到了问题。我的python脚本查询这些元组。这里不是我的输出: CENPVP2 441495 9606 NR_033773.1 None NC_000023.11 None CENPVP2 441495 9606 NR_033773.1 None NT_011630.15 None CENPVP2 441495 9606 None None NG_02259

我目前正在为一个bioinfomatics类处理数据库,我在格式化SQL输出时遇到了问题。我的python脚本查询这些元组。这里不是我的输出:

CENPVP2 441495  9606    NR_033773.1 None    NC_000023.11    None
CENPVP2 441495  9606    NR_033773.1 None    NT_011630.15    None
CENPVP2 441495  9606    None    None    NG_022599.1 None
CT47A11 255313  9606    NM_173571.2 NP_775842.2 NC_000023.11    12477932
CT47A11 255313  9606    NM_173571.2 NP_775842.2 NC_000023.11    16382448
CT47A11 255313  9606    NM_173571.2 NP_775842.2 NC_000023.11    18976975
CT47A11 255313  9606    NM_173571.2 NP_775842.2 NG_027735.1 12477932
CT47A11 255313  9606    NM_173571.2 NP_775842.2 NG_027735.1 16382448
CT47A11 255313  9606    NM_173571.2 NP_775842.2 NG_027735.1 18976975
CT47A11 255313  9606    NM_173571.2 NP_775842.2 NT_011786.17    12477932
CT47A11 255313  9606    NM_173571.2 NP_775842.2 NT_011786.17    16382448
CT47A11 255313  9606    NM_173571.2 NP_775842.2 NT_011786.17    18976975
CT47A11 255313  9606    None    None    NG_027735.1 12477932
CT47A11 255313  9606    None    None    NG_027735.1 16382448
CT47A11 255313  9606    None    None    NG_027735.1 18976975
其中每个字段由制表符分隔,我需要这样做,其中每个字段仍由制表符分隔,但现在有多值字段,其中值由管道分隔,空值由破折号表示:

CENPVP2 441495 9606 NR_033773.1 - NC_000023.11|NG_022599.1|NT_011630.15 -
CT47A11 255313 9606 NM_173571.2 NP_775842.2 NC_000023.11|NG_027735.1|NT_011786.17 12477932|16382448|18976975
设置输出格式以匹配第二个表的最佳方式是什么

以下是我的python脚本:

   import sys
   import getopt
   import psycopg2

def writerows(row, outFile):
    outFile.write("%s\t" % row[1])
    outFile.write("%s\t" % row[0])
    outFile.write("%s\t" % row[2])
    outFile.write("%s\t" % row[3])
    outFile.write("%s\t" % row[5])
    outFile.write("%s\t" % row[4])
    outFile.write("%s\n" % row[6])

def usage(err):
    print("I will handle this later")

def main():
    inFile = sys.stdin
    outFile = sys.stdout

try:
    opts, args = getopt.getopt(sys.argv[1:], "i:o:")
except getopt.GetoptError as err:
    usage(err)
    sys.exit(2)
for (opt, arg) in opts:
    if(opt == "-i"):
        inFile = open(arg, "r")
    if(opt == "-o"):
        outFile = open(arg, "w")

line = inFile.readline()
line = line.replace("\n", "")
conn = psycopg2.connect("dbname=********* user=*********** "
                        "password=********** host=localhost")
cursor = conn.cursor()
    while (line):
    cursor.execute("SELECT DISTINCT geneinfo.gene_id, geneinfo.symbol, "
                   "geneinfo.tax_id, gene2refseq.rna_accession, "
                   "gene2refseq.gen_accession, "
                   "gene2refseq.pro_accession, gene2pubmed.pubmed_id FROM "
                   "geneinfo LEFT JOIN gene2refseq ON "
                   "geneinfo.gene_id = gene2refseq.gene_id LEFT JOIN "
                   "gene2pubmed ON geneinfo.gene_id = gene2pubmed.gene_id "
                   "WHERE geneinfo.symbol ILIKE '"+line+"' OR "
                   "geneinfo.synonyms ILIKE '%"+line+"%' ORDER BY "
                   "geneinfo.symbol ASC, geneinfo.tax_id ASC;")
    result = cursor.fetchone()

    if result:
        while result:
            writerows(result, outFile)
            result = cursor.fetchone()
    else:
        outFile.write("\n")

    line = inFile.readline()
    line = line.replace("\n", "")

cursor.close()
conn.close()

   if (__name__=='__main__'):
    main()

帕菲在评论中说了些什么。查询将类似于:

SELECT DISTINCT gi.gene_id, gi.symbol, gi.tax_id,
    rs.rna_accession, rs.gen_accession,
    array_to_string(array_agg(rs.pro_accession), '|')),
    array_to_string(array_agg(pm.pubmed_id), '|'))
FROM geneinfo gi
LEFT JOIN gene2refseq rs ON gi.gene_id=rs.gene_id
LEFT JOIN gene2pubmed pm ON gi.gene_id=pm.gene_id
WHERE gi.symbol ILIKE 'SOMESTUFF' OR
gi.synonyms ILIKE 'OTHERSTUFF'
GROUP BY 1,2,3,4,5
ORDER BY gi.symbol ASC, gi.tax_id ASC;

元组在哪里?请说明数据是如何导出的?从数据库?从文本文件?来自熊猫?工作脚本有助于再现性。数据来自数据库,但特定的基因符号查询来自文本文件,因此有“opts”参数。调整SQL。看看吧谢谢你,冻糕!我试试看。