繁体   English   中英

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

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

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

我正在连接到Oracle数据库并检索一些记录。 可以从数据库中两次或多次获取具有准确时间戳,值等的相同记录。 我来自R编程,可以在其中向数据帧发出唯一命令以完成此任务。

我如何确保每个唯一字段在pyton中仅打印一次。 这是我的代码:

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"

输出为:

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)

您可以在这里使用set

在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