简体   繁体   English

您如何确保每个唯一的行在python中打印一次

[英]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: 我如何确保每个唯一的行在python中仅打印一次:

I am connecting to Oracle db and retrieve some records. 我正在连接到Oracle数据库并检索一些记录。 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. 我来自R编程,可以在其中向数据帧发出唯一命令以完成此任务。

how could I make sure that each unique field is printed only once in pyton. 我如何确保每个唯一字段在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. 您可以在这里使用set

Use select distinct in your sql query. 在SQL查询中使用select unique。

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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