简体   繁体   中英

How do you ensure that each unique line printed once in python

How would I make sure that each unique line is printed only once in python:

I am connecting to Oracle db and retrieve some records. It is possible that the same record with exact time stamp, value etc may be retrieved from the db twice or muliple times. I come from R programming where I could issue unique command to the data frame to accomplish this.

how could I make sure that each unique field is printed only once in pyton. This is my code:

import pyodbc
import re
sql="DateTime, Server, Server_Type, Metric, Value from oracle_table"

cnxn = pyodbc.connect("DSN=dsn1;UID=userid;PWD=passwd123")

cursor = cnxn.cursor()


cursor.execute(sql)
row = cursor.fetchall()

for line in row:
   if line[4]:
        if float(line[4])>=0:
            print line[1]+"."+re.sub(r'\W+', '', re.sub(r'\%', 'Percent', line[3])),line[0].strftime('%s'), ("%.6f" % float(line[4])), "host="+re.sub("\..*$","",line[1]), "type="+line[2],"source=Oracle","dc=DC1"

Output is:

server1.CRITICAL_INCIDENTS 1418223897 0.000000 host=server1 type=oracle_database source=Oracle dc=DC1
server1.ResponseTimepertransaction 1418223577 2.467900 host=server1 type=oracle_database source=Oracle dc=DC1
server1.DataDictionaryHitPercent 1418223577 100.000000 host=server1 type=oracle_database source=Oracle dc=DC1
server1.FullIndexScanspersecond 1418223577 0.000000 host=server1 type=oracle_database source=Oracle dc=DC1
server1.ExecutesPerformedwithoutParsesPercent 1418223577 66.666667 host=server1 type=oracle_database source=Oracle dc=DC1
server1.SortsinMemoryPercent 1418223577 100.000000 host=server1 type=oracle_database source=Oracle dc=DC1
server1.BufferCacheHitPercent 1418223577 100.000000 host=server1 type=oracle_database source=Oracle dc=DC1
server1.DatabaseCPUTimePercent 1418223577 81.048665 host=server1 type=oracle_database source=Oracle dc=DC1
server1.CRITICAL_INCIDENTS 1418223897 0.000000 host=server1 type=oracle_database source=Oracle dc=DC1
server1.CRITICAL_INCIDENTS 1418223897 0.000000 host=server1 type=oracle_database source=Oracle dc=DC1
server1.ResponseTimepertransaction 1418223577 2.467900 host=server1 type=oracle_database source=Oracle dc=DC1
# Python 2.7
seenAlready = set()
for line in row:
    if line[4]:
        if float(line[4])>=0:
            outputLine = ... # Whatever you do to construct the output line
            if outputLine not in seenAlready:
                print outputLine
                seenAlready.add(outputLine)
ll=[]
for line in row:
   if line[4]:
        if float(line[4])>=0:
            ll.append(line[1]+"."+re.sub(r'\W+', '', re.sub(r'\%', 'Percent', line[3])),line[0].strftime('%s'), ("%.6f" % float(line[4])), "host="+re.sub("\..*$","",line[1]), "type="+line[2],"source=Oracle","dc=DC1")

print set(ll)

You can use set here.

Use select distinct in your sql query.

import pyodbc
import re
sql = """select distinct DateTime, Server, Server_Type, Metric, Value 
     from oracle_table where Value >= 0"""

cnxn = pyodbc.connect("DSN=dsn1;UID=userid;PWD=passwd123")

cursor = cnxn.cursor()

cursor.execute(sql)
row = cursor.fetchall()

for line in row:
    print line[1]+"."+re.sub(r'\W+', '', re.sub(r'\%', 'Percent', line[3])),
        line[0].strftime('%s'), ("%.6f" % float(line[4])), 
        "host="+re.sub("\..*$","",line[1]), 
        "type="+line[2],"source=Oracle","dc=DC1"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM