Unable to save csv file with Pandas - python

Sorry for the dummy question but I read lots of topics but my code still do not create and save a .csv file.
import pandas as pd
def save_csv(lista):
try:
print("Salvando...")
name_path = time.strftime('%d%m%y') + '01' + '.csv'
df = pd.DataFrame(lista, columns=["column"])
df.to_csv(name_path, index=False)
except:
pass
dados = [-0.9143399074673653, -1.0944355744868517, -1.1022400576621294]
save_csv(dados)
Path name is 'DayMonthYear01.csv' (20121701.csv).
When I run the code it finishes but no file is saved.
The output of the code is just:
>>>
RESTART: C:\Users\eduhz\AppData\Local\Programs\Python\Python36-32\testeCSV.py
Salvando...
>>>
Does anyone knows what am I missing?

First, as answered by #Abdou I changed the code to provide me what was the error.
import pandas as pd
import time
def save_csv(lista):
try:
print("Salvando...")
name_path = time.strftime('%d%m%y') + '01' + '.csv'
df = pd.DataFrame(lista, columns=["column"])
df.to_csv(name_path, index=False)
except Exception as e:
print(e)
dados = [-0.9143399074673653, -1.0944355744868517, -1.1022400576621294]
save_csv(dados)
Then I found out it was due to a permission error
[Errno 13] Permission denied:
caused by the fact Notepad (without being opened as Administrator) does not have access to some directories and therefore anything run inside it wouldn't be able to write to those directories.
I tried running Notepad as administrator but it didn't work.
The solution was running the code with the Python IDLE.

Did you import the time module? All i did was add that and it made a 21121701.csv with the 3 entries in one columns in the current working directory.
import pandas as pd
import time
def save_csv(lista):
print("Salvando...")
name_path = time.strftime('%d%m%y') + '01' + '.csv'
df = pd.DataFrame(lista, columns=["column"])
df.to_csv(name_path, index=False)
dados = [-0.9143399074673653, -1.0944355744868517, -1.1022400576621294]
save_csv(dados)
Removing the try/except gives a file permission error if you have a file of the same name already open. You have to close any file you are trying to write (on windows at least).
Per Abdou's comment, if you (or the program) don't have write access to the directory then that would cause a permission error too.

Related

python runs but stop working and nothing happens

I am trying to run the code, but it stops working when runs to print('continue') and then nothing happens at all. I have tried to install new python version and also other programs with classes work.
Please, give me some hint why this doesn't run properly.
import os
import os.path
import openpyxl
from openpyxl import load_workbook
os.chdir('/Users/ns.blinnikova/Desktop')
class SearchTeachers:
def __init__(self, sheetFileName, sheetGoogleName):
self.sheetWithTeachers = load_workbook(sheetFileName)['Лист1']
self.sheet_to_work = load_workbook(sheetGoogleName)['Data']
self.end_row = self.sheet_to_work.max_row
self.n = 1
def createFile(self, filenumber):
self.filenumber = filenumber
newFile = 'phd' + str(self.filenumber) + '.csv'
self.FillFile = open(newFile, 'w')
self.FillFile.write("username;password;course1;type1")
self.FillFile.write('\n')
print("file is done" + newFile)
return self.FillFile
filenum = 1
print('continue')
wobject = SearchTeachers('somefile.xlsx', 'another file.xlsx')
print(wobject)
fillingFile = wobject.createFile(filenum)
Is the Serbian for Sheet1 (Лист1) suppose to be there?
Do the two XLSX files exist on the desktop (as chdir suggests)?
Consider first trying the load_workbook functions instead of creating wobject -- just as a sanity check.

Python pathlib not accepted by Pandas.ExcelWriter(..)

After finally getting Pandas to output my file as a spreadsheet (silly issue), I ran into an issue with the file path input not being read properly, even as a raw string. Eventually I stumbled upon the pathlib library, and it's been successful in allowing me to read and write to my file as needed. However, now I'm back to ExcelWriter complaining about my file again.
Here's my ugly attempt at using pathlib to create the paths (directories substituted):
import pandas as pd
core = input("Core number ('core_#k'): ")
part = input("Part name ('part_#'): ")
file_level = 'core_' + core + 'k'
in_file_name = file_level + '_part_' + part + '.txt'
out = in_file_name[:-4] + '.ods'
# format filenames
path_raw = Path("DRIVELABEL:\someDirectories\\")
raw_text_path = Path(path_raw) / "raw_text" / file_level / in_file_name
spreadsheet_path = Path(path_raw) / "spreadsheets" / file_level / out
Error:
FileNotFoundError: [Errno 2] No such file or directory: 'DRIVELABEL:/SomeDirectories/outputFile.ods'
I can't figure out how to get ExcelWriter to write this file properly. The only thing I can think right now is that the file_level for spreadsheet_path doesn't exist yet, but shouldn't it be created at write?
with pd.ExcelWriter(spreadsheet_path.as_posix(), engine='odf') as doc:
df.to_excel(doc, sheet_name="Sheet1", index=False)

Pandas, Python - Problem with converting xlsx to csv

I found to have problem with conversion of .xlsx file to .csv using pandas library.
Here is the code:
import pandas as pd
# If pandas is not installed: pip install pandas
class Program:
def __init__(self):
# file = input("Insert file name (without extension): ")
file = "Daty"
self.namexlsx = "D:\\" + file + ".xlsx"
self.namecsv = "D:\\" + file + ".csv"
Program.export(self.namexlsx, self.namecsv)
def export(namexlsx, namecsv):
try:
read_file = pd.read_excel(namexlsx, sheet_name='Sheet1', index_col=0)
read_file.to_csv(namecsv, index=False, sep=',')
print("Conversion to .csv file has been successful.")
except FileNotFoundError:
print("File not found, check file name again.")
print("Conversion to .csv file has failed.")
Program()
After running the code the console shows the ValueError: File is not a recognized excel file error
File i have in that directory is "Daty.xlsx". Tried couple of thigns like looking up to documentation and other examples around internet but most had similar code.
Edit&Update
What i intend afterwards is use the created csv file for conversion to .db file. So in the end the line of import will go .xlsx -> .csv -> .db. The idea of such program came as a training, but i cant get past point described above.
You can use like this-
import pandas as pd
data_xls = pd.read_excel('excelfile.xlsx', 'Sheet1', index_col=None)
data_xls.to_csv('csvfile.csv', encoding='utf-8', index=False)
I checked the xlsx itself, and apparently for some reason it was corrupted with columns in initial file being merged into one column. After opening and correcting the cells in the file everything runs smoothly.
Thank you for your time and apologise for inconvenience.

PANDAS: Execute to_excel successfully,but no output file

When I use to_excel to generate excel file, no error occurred , but no output file available. can't find anything,any file in my supposed location...
And I don't know why..
import pandas as pd
import os
import xlrd
import openpyxl
pd.set_option('display.width',None)
DIR = 'E:\Process'
datapath =os.path.join(DIR, 'Data.xlsx')
formatpath = os.path.join(DIR, 'Format.xlsx')
df = pd.read_excel(datapath)
df1=pd.read_excel(formatpath)
for i in range(0, len(df)):
target = (df.iloc[i,17])
df2 = df1
df2.iat[3,3] = target
print(df2)
filename = df.iloc[i,2]
filename = str(filename) + ".xlsx"
sourcepath = os.path.join(DIR, filename)
writer = pd.ExcelWriter(sourcepath)
df2.to_excel(writer)
print(sourcepath)
Expanding on the comment:
Use writer.save() after calling to_excel.
Alternatively you can use the with statement as suggested in the docs: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.ExcelWriter.html
When I'm trying to re-run this code on my local machine, I'm running into an error with setting DIR to 'E:\Process' because of the escaping of "\" for windows machines. Have you tried "E:\\Process"? or try it as a raw string literal r'E:\Process'?

Python APscheduler and returning filepath of most recent csv in directory

I am trying to get the below code to write files to the specified folder with no luck. I think the error is with the imported 'glob' package/function because similar code works for other files, but I'm not sure. Note also that I'm not getting any errors on the in-between 'do stuff' code so I don't think that's an issue.
#Import Stuff
import pandas as pd
import os
#Import apscheduler and related packages
import time
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.interval import IntervalTrigger
def process_ZN_ES_comb_LL_15M_csv(path_to_csv):
# Open ZN_ES_comb and customize
filename2 = max(glob.iglob("C:\Users\cost9\OneDrive\Documents\PYTHON\Daily Tasks\ZN_ES\ZN_ES_15M\CSV\Beta\*.csv"))
ZN_ES_comb_LL_15M = pd.read_csv(filename2)
#Do stuff, no errors given
#Send to csv automatically
ZN_ES_comb_LL_15M.to_csv(path_to_csv.replace('.csv', '_modified_{timestamp}.csv').format(
timestamp=time.strftime("%Y%m%d-%H%M%S")), index=False)
if __name__ == '__main__':
path_to_csv = "C:\Users\cost9\OneDrive\Documents\PYTHON\Daily Tasks\ZN_ES\ZN_ES_15M\CSV\Lead_Lag\ZN_ES_comb_LL_15M.csv"
scheduler = BackgroundScheduler()
scheduler.start()
scheduler.add_job(func=process_ZN_ES_comb_LL_15M_csv,
args=[path_to_csv],
trigger=IntervalTrigger(seconds=60))
# Wait for 7 seconds so that scheduler can call process_csv 3 times
time.sleep(7)
Essentially I'm having apscheduler automatically write the file to the folder shown below, but nothing is showing up. Further, I have to identify a file using 'glob' package from another folder in order to build on that file in the #do stuff lines. That's why I think there's some issue with the filename2 line but I'm not sure. Any help is appreciated!
Try use double quotes " for your filename2 line. The thing that is jumping out at me is the whitespace in the file path "Daily Tasks" and using double quotes can solve this issue.

Categories

Resources