我想寫2個變量到我的CSV中的行。 result
變量(它有2列/值)和'item variable (which has just 1 value). Can I include both of these values in the
writerow`函數參數?如果是這樣,語法將如何顯示?我的語法如下,兩個變量之間的逗號不起作用。CSV WriteRow數據
行我說的是:
writer.writerow([item,result.items()[0][0],result.items()[0][1]])
的所有代碼:
#!/usr/bin/env python
import splunklib.client as client
import splunklib.results as results
import csv
listOfAppIDs = []
with open('filefolder/filelocation.txt', 'r') as fi:
for line in fi:
listOfAppIDs.append(line.rstrip('\n'))
listOfAppIDs.sort() #alphebetically sort list
HOST = "ipAddress"
PORT = 1234
USERNAME = "uName"
PASSWORD = "pWord"
startPoint = "firstAppInList"
outputCsv = open('filefolder/filelocation.csv', 'wb')
fieldnames = ['appID', 'TotalDestinationIPAddresses', 'AverageThroughputPerMonth']
writer = csv.DictWriter(outputCsv, fieldnames=fieldnames)
writer.writeheader();
def connect():
global startPoint , item
service = client.connect(
host=HOST,
port=PORT,
username=USERNAME,
password=PASSWORD,
autologin=True
)
jobs = service.jobs
kwargs_blockingsearch = {"exec_mode": "blocking"}
try:
for item in listOfAppIDs:
if (item >= startPoint):
searchquery_blocking = "splunkQuery"
print item + ':'
job = jobs.create(searchquery_blocking, **kwargs_blockingsearch)
for result in results.ResultsReader(job.results()):
writer.writerow([item,result.items()[0][0],result.items()[0][1]])#writes data underneath the column header
except csv.Error:
startPoint = item
connect()
connect()
outputCsv.close()
答:
writer.writerow([item,result.items()[0][1],result.items()[1][1]])
謝謝您的輸入。 'Result'實際上是一個有序的字典,那麼我怎樣在'result'數組的第一項中調用鍵/值對的值?也許像'result.keys(0)'和'result.keys(1)'? – pHorseSpec
您可以通過'result.items()' – CaptSolo
獲得鍵/值對我已經使用@ CaptSolo的建議更新了該帖子。 – Jaco