<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Python SDK save search to csv in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/Python-SDK-save-search-to-csv/m-p/420167#M7370</link>
    <description>&lt;P&gt;Thanks @poete!&lt;/P&gt;

&lt;P&gt;Here is what I used in the end&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;results_kwargs = {
 "earliest_time": "-40min",
 "latest_time": "now",
 "search_mode": "normal",
 "output_mode": "csv"
}
oneshotsearch_results = service.jobs.oneshot(query, **results_kwargs)
f=open('myresults.csv', 'w')
f.write(oneshotsearch_results.read())
f.close()
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 10 Aug 2018 18:28:24 GMT</pubDate>
    <dc:creator>to914868</dc:creator>
    <dc:date>2018-08-10T18:28:24Z</dc:date>
    <item>
      <title>Python SDK save search to csv</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Python-SDK-save-search-to-csv/m-p/420164#M7367</link>
      <description>&lt;P&gt;I want to use splunklib to run a one-off Splunk query and save it to csv.&lt;BR /&gt;
I'm testing with a small query (a single visitId) of 8 events only. &lt;BR /&gt;
The result is returned immediately in Splunk UI but I have problems getting the result from the python-sdk. &lt;/P&gt;

&lt;P&gt;My problems with splunklib are: &lt;BR /&gt;
- &lt;STRONG&gt;&lt;EM&gt;service.jobs.export()&lt;/EM&gt;&lt;/STRONG&gt; query does not complete because it keeps repeating the same 8 event results over and over again&lt;BR /&gt;
- &lt;STRONG&gt;&lt;EM&gt;service.jobs.oneshot()&lt;/EM&gt;&lt;/STRONG&gt; query does not finish and returns no result&lt;/P&gt;

&lt;P&gt;I tried adding the search parameters &lt;EM&gt;"preview"=False&lt;/EM&gt;, i.e.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;kwargs_export = { "search_mode": "normal","preview": True }
rr = results.ResultsReader(service.jobs.export(query,**kwargs_export ))
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The only effect is that neither option returns anything anymore, since the queries are not completing. &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import splunklib.client as client
import splunklib.results as results
service = client.connect( 
               host=HOST, 
               port=8089,
               username=USERNAME,
               password=PWD )
    query= """search index=xxx application="xxx" sourcetype=xxx| 
    spath visitId  | join type ..."""
    rr = results.ResultsReader(service.jobs.export(query))

    for item in rr:
        for key in item.keys():
            print(key, len(item[key]), item[key])
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I tried the same with oneshot&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;kwargs_oneshot = {'output_mode': 'csv',"search_mode": "normal"}
oneshotsearch_results = service.jobs.oneshot(query, **kwargs_oneshot)
 f=open('myresults.csv', 'w')
 f.write(oneshotsearch_results.read())
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This creates a csv file but has no content at all. I think .read is deprecated.&lt;BR /&gt;
&lt;STRONG&gt;Any suggestions ?&lt;/STRONG&gt;&lt;BR /&gt;
All I want is to save the query results to .csv ONCE using the library.&lt;BR /&gt;
Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jul 2018 10:42:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Python-SDK-save-search-to-csv/m-p/420164#M7367</guid>
      <dc:creator>to914868</dc:creator>
      <dc:date>2018-07-04T10:42:50Z</dc:date>
    </item>
    <item>
      <title>Re: Python SDK save search to csv</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Python-SDK-save-search-to-csv/m-p/420165#M7368</link>
      <description>&lt;P&gt;try:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;kwargs_export = { "output_mode": "csv"}
rr = service.jobs.export(query)

for item in rr:
    print(item)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I think that you shouldn't need to convert the result into resultsreader because it already is one.&lt;/P&gt;</description>
      <pubDate>Mon, 16 Jul 2018 19:41:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Python-SDK-save-search-to-csv/m-p/420165#M7368</guid>
      <dc:creator>evuk</dc:creator>
      <dc:date>2018-07-16T19:41:13Z</dc:date>
    </item>
    <item>
      <title>Re: Python SDK save search to csv</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Python-SDK-save-search-to-csv/m-p/420166#M7369</link>
      <description>&lt;P&gt;Hello @to914868,&lt;/P&gt;

&lt;P&gt;please add &lt;CODE&gt;f.close()&lt;/CODE&gt; on the next line after &lt;CODE&gt;f.write(oneshotsearch_results.read())&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;I think the content is not flushed to the file.&lt;/P&gt;

&lt;P&gt;@to914868, please accet this answer in order for other users to find more easily the answer to this question.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jul 2018 06:37:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Python-SDK-save-search-to-csv/m-p/420166#M7369</guid>
      <dc:creator>poete</dc:creator>
      <dc:date>2018-07-17T06:37:10Z</dc:date>
    </item>
    <item>
      <title>Re: Python SDK save search to csv</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Python-SDK-save-search-to-csv/m-p/420167#M7370</link>
      <description>&lt;P&gt;Thanks @poete!&lt;/P&gt;

&lt;P&gt;Here is what I used in the end&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;results_kwargs = {
 "earliest_time": "-40min",
 "latest_time": "now",
 "search_mode": "normal",
 "output_mode": "csv"
}
oneshotsearch_results = service.jobs.oneshot(query, **results_kwargs)
f=open('myresults.csv', 'w')
f.write(oneshotsearch_results.read())
f.close()
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 10 Aug 2018 18:28:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Python-SDK-save-search-to-csv/m-p/420167#M7370</guid>
      <dc:creator>to914868</dc:creator>
      <dc:date>2018-08-10T18:28:24Z</dc:date>
    </item>
    <item>
      <title>Re: Python SDK save search to csv</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Python-SDK-save-search-to-csv/m-p/420168#M7371</link>
      <description>&lt;P&gt;This works.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 18:31:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Python-SDK-save-search-to-csv/m-p/420168#M7371</guid>
      <dc:creator>kkrishnan_splun</dc:creator>
      <dc:date>2018-10-30T18:31:58Z</dc:date>
    </item>
    <item>
      <title>Re: Python SDK save search to csv</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Python-SDK-save-search-to-csv/m-p/420169#M7372</link>
      <description>&lt;P&gt;This is working fine , But i could not fetch all the results in csv. Kindly provide me the solution for this question &lt;BR /&gt;
&lt;A href="https://answers.splunk.com/answers/708529/export-to-csv-is-not-fetching-all-the-results-pyth.html?minQuestionBodyLength=80"&gt;https://answers.splunk.com/answers/708529/export-to-csv-is-not-fetching-all-the-results-pyth.html?minQuestionBodyLength=80&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Dec 2018 14:18:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Python-SDK-save-search-to-csv/m-p/420169#M7372</guid>
      <dc:creator>pchp348</dc:creator>
      <dc:date>2018-12-10T14:18:13Z</dc:date>
    </item>
  </channel>
</rss>

