1

I have built a program that does some data analysis and then saves it to a CSV on my desktop.

The problem is, the path for my desktop has my name in and I wont know the path of anyone else who uses it (i.e. it wont work).

Is there a way of opening a new CSV and inserting the data into it (and letting them save it), rather than the program saving it?

My code is:

import pandas as pd
import pyodbc

d=[]

key1 = raw_input('enter a keyword to search for: ')
key2 = raw_input('enter another keyword to search for: ')


conn = pyodbc.connect('DSN=QueryBuilder')
cursor = conn.cursor()

stringQ ="SELECT GrantInformation.GrantRefNumber, GrantInformation.PIName, GrantInformation.Call, GrantInformation.RoutingClassification, GrantInformation.GrantCategory, GrantInformation.AuthorisationDate, GrantInformation.HoldingOrganisationName, GrantInformation.StatusGeneral, GrantInformation.GrantTitle, GrantSummary.Summary, GrantDates.ActualStartDate, GrantDates.ActualEndDate, GrantInformation.TotalGrantValue FROM (GrantInformation LEFT JOIN GrantSummary ON GrantInformation.GrantRefNumber = GrantSummary.GrantRefNumber) LEFT JOIN GrantDates ON GrantInformation.GrantRefNumber = GrantDates.GrantRefNumber WHERE (((GrantInformation.AuthorisationDate)>='2005/4/1') AND ((GrantInformation.StatusGeneral) Like '%auth%') AND ((GrantInformation.GrantTitle) Like '%{}%'AND (GrantInformation.TransferInd)= 'false' OR (GrantInformation.GrantTitle) Like '%{}%') AND ((GrantInformation.TransferInd)= 'false'))  OR (((GrantInformation.AuthorisationDate)>='2005/4/1') AND ((GrantInformation.StatusGeneral) Like '%auth%') AND ((GrantSummary.Summary) Like '%{}%'AND (GrantInformation.TransferInd)= 'false' OR (GrantSummary.Summary) Like '%{}%' AND (GrantInformation.TransferInd)= 'false'));".format(key1,key2,key1,key2)

cursor.execute(stringQ)

rows = cursor.fetchall()



for row in rows:
    d.append({'GrantRefNumber':row[0],'Call':row[2],'Classification':row[3],'Grant Category':row[4],'Authorisation Date':row[5],'Organisation':row[6],'Status General':row[7],'Grant Title':row[8],'Summary':row[9],'Start Date':row[10],'End Date':row[11],'Total Value':row[12]})

df = pd.DataFrame(d)
new_df = df[['GrantRefNumber','Grant Title','Organisation','Call','Grant Category','Authorisation Date','Status General','Total Value','Classification','Start Date','End Date','Summary']]
new_df.to_csv("C:/Users/nicholas/Desktop/data.csv", header=True, index=False, encoding='utf-8')

1 Answer 1

2

you can use dynamic paths:

either save to your current working directory:

new_df.to_csv("data.csv", header=True, index=False, encoding='utf-8')

or use os.getcwd() to control the directory:

import os
new_df.to_csv(os.getcwd()+"data.csv", header=True, index=False, encoding='utf-8')
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, thank you!! :)... didnt realise it was so simple

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.