如何使用python

时间:2015-08-04 22:06:46

标签: python file

我有一个运行sql的代码,并从第一列

获取以下详细信息
U_PATIENTID FNAME   LNAME   COLLECTIONDT    ENTEREDTEXT
PT0001981   Al       Calino 26-Aug-14         89
PT0001982   ALICA    BAU    19-Apr-15         90
PT0001983   DUSYA   AGRIGAT 27-Aug-14         86
PT0001983   DUSYA   AGRIGAT 16-Apr-15         85
PT0001985   VIKI    BERES   17-Apr-15         93
PT0001986   ZUZU    GIGI    16-Apr-15         84

所以,如果你看到PT0001983重复了2次,而不是多行,我想在

中输出这样的输出
PT0001983   DUSYA   AGRIGAT 27-Aug-14 86 16-Apr-15 85

简而言之,只要PT重复,我只需要将它们移动到一行而不是多行

    import cx_Oracle
    import sys
    import fileinput

    # ORACLE CONNECTION HERE


    ####################################

    def getPatientDemo( db, ptid):

    sql = """
    SQL SCRIPT HERE
    """%(ptid)

    stmt = db.prepare(sql)
    db.execute(stmt)
    ptid = "*"
    fname = "*"
    lname = "*"
    tstdt = "*"
    score = "*"
    for row in db:
        ptid = row[0]
        fname = row[1]
        lname = row[2]
        tstdt = row[3]
        scor e = row[4]
    return (ptid, fname, lname, tstdt, score)


    ####################################

if __name__ == '__main__':


#PtIdNumHash = ()
f = open("somecsv.csv",'r')
out = f.readlines()
for line in out:
        ptid = line.strip()

         demo = getPatientDemo(curCLIMS, ptid)
        (ptid, fname, lname, colldt, score) = demo






      print "%s,%s,%s,%s"%(ptid, fname, lname, colldt, score)


 ClOSE DB CONNECTION

由于

1 个答案:

答案 0 :(得分:0)

您需要收集结果,然后进行打印,而不是同时进行打印。

#PtIdNumHash = ()
f = open("somecsv.csv",'r')
out = f.readlines()
dbresultprint = []
for line in out:
    ptid = line.strip()
    duplicate = false
    result = getPatientDemo(curCLIMS, ptid)
    for dbr in dbresultprint:
        if ptid in dbr:
            change = list(dbr)
            change[3] = dbr[3] + " " + result[3]
            dbr = tuple(change)
        else:
            dbresultprint.append(result)

for result in dbresultprint:
    print "%s,%s,%s,%s"%result