AttributeError: 'file' object has no attribute 'DictReader' - python

I'm creating a temporary CSV file:
for formname in formnamesFinal:
csv = tempfile.NamedTemporaryFile("w", prefix=formname+'_', suffix=".csv", dir = "/var/tmp/")
csv.write(....)
And I'm writing something in it. Now I want to read this file with DictReader:
content = csv.DictReader(csv, delimiter=';')
for contenthelp in content:
contentlist.append(contenthelp)
But I'm receiving the following error:
AttributeError: 'file' object has no attribute 'DictReader'
I have to step through the temp CSV files, because I have huge datasets to get from a database for the following steps and it would take too much time to load the data over and over.

csv = tempfile.NamedTemporaryFile("w", prefix=formname+'_', suffix=".csv", dir = "/var/tmp/")
This line overwrites your reference to the csv module. Try renaming it to something else.
my_csv = tempfile.NamedTemporaryFile("w", prefix=formname+'_', suffix=".csv", dir = "/var/tmp/")
Now you should be able to access csv properly again.

Another cause for this error is there is a python script with a filename of csv.py.
This hides the name of Python's built-in csv module.
Resolve the issue by renaming the user created csv.py script file.

Related

how to write csv to "variable" instead of file?

I'm not sure how to word my question exactly, and I have seen some similar questions asked but not exactly what I'm trying to do. If there already is a solution please direct me to it.
Here is what I'm trying to do:
At my work, we have a few pkgs we've built to handle various data types. One I am working with is reading in a csv file into a std_io object (std_io is our all-purpose object class that reads in any type of data file).
I am trying to connect this to another pkg I am writing, so I can make an object in the new pkg, and covert it to a std_io object.
The problem is, the std_io object is meant to read an actual file, not take in an object. To get around this, I can basically write my data to temp.csv file then read it into a std_io object.
I am wondering if there is a way to eliminate this step of writing the temp.csv file.
Here is my code:
x #my object
df = x.to_df() #object class method to convert to a pandas dataframe
df.to_csv('temp.csv') #write data to a csv file
std_io_obj = std_read('temp.csv') #read csv file into a std_io object
Is there a way to basically pass what the output of writing the csv file would be directly into std_read? Does this make sense?
The only reason I want to do this is to avoid having to code additional functionality into either of the pkgs to directly accept an object as input.
Hope this was clear, and thanks to anyone who contributes.
For those interested, or who may have this same kind of issue/objective, here's what I did to solve this problem.
I basically just created a temporary named file, linked a .csv filename to this temp file, then passed it into my std_read function which requires a csv filename as an input.
This basically tricks the function into thinking it's taking the name of a real file as an input, and it just opens it as usual and uses csvreader to parse it up.
This is the code:
import tempfile
import os
x #my object I want to convert to a std_io object
text = x.to_df().to_csv() #object class method to convert to a pandas dataframe then generate the 'text' of a csv file
filename = 'temp.csv'
with tempfile.NamedTemporaryFile(dir = os.path.dirname('.')) as f:
f.write(text.encode())
os.link(f.name, filename)
stdio_obj = std_read(filename)
os.unlink(filename)
del f
FYI - the std_read function essentially just opens the file the usual way, and passes it into csvreader:
with open(filename, 'r') as f:
rdr = csv.reader(f)

How to get the binary contents of a pandas xlsxwriter object for dropbox upload

I am trying to upload to dropbox an Excel sheet with multiple tabs made with pandas and xlsxwriter. Not very big files, just around 5MB. I am using the dropbox module for Python 3
I am doing:
filename = pd.ExcelWriter('myfile.xlsx' ,engine='xlsxwriter')
actions.to_excel(filename, sheet_name="Combined Actions")
filename.save()
with open(filename, "rb") as f:
dbx.files_upload(f.read(), "/MyAPP/"+filename)
I read in the docs that files_upload expects a bytes object. I am getting the error: Type Error: Invalid file: <pandas.io.excel__XlsxWriter object at 0x000000000071C48>
Is this error because I am not supplying the file in the needed format? How can I do this upload so I don't get a type error?
You are trying to use the built in function open to open an object. It requires a string as its first argument (which represents a file path). More information on the open function can be found in the docs.
The source for pandas ExcelWriter shows it stores the filename you pass to it in .path. So renaming your variables to better represent what they are and using the .path attribute of the ExcelWriter instance:
excel_file = pd.ExcelWriter('myfile.xlsx', engine='xlsxwriter')
actions.to_excel(excel_file, sheet_name="Combined Actions")
excel_file.save()
with open(excel_file.path, 'rb') as f:
dbx.files_upload(f.read(), '/MyAPP/'+excel_file.path)

Open excel output with xlsxwriter from temporary file fail

I'm creating a xlsx output with xlsxwriter into a temporary file using tempfile module, I store the path to this temporary file inside a variable that I later use in another script to open it.
The problem is that sometimes opening the file fails with the error :
"[Errno 2] No such file or directory: '/tmp/xls5TnVsx'"
Sorry I don't have an exact idea about the frequency of this problem occurring but it seems like it happens from time to time, so I don't understand why...
This is how I save into a temporary file :
f = tempfile.NamedTemporaryFile(prefix="xls",delete=False)
xlsfilename = f.name
Then to create the xlsx output :
wb = xlsxwriter.Workbook(filename)
ws = wb.add_worksheet(sheetName)
# Write header
....
# Write data
for row, row_data in enumerate(data, start=1):
for column, key in enumerate(headers):
....
wb.close()
f.close()
Then in a Python CGI script I use the variable xlsxfilename which is the path to the script to open it :
print "Content-type: application/msexcel"
print "Content-Disposition: attachment; filename="+xlsfilename
print
try :
print open(xlsfilename,"rb").read()
finally:
try:
xlsfilename.close()
except:
pass
os.unlink(xlsfilename)
What am I doing wrong here and any ideas on how to solve this by maybe using another method to storing into a temporary file?
I believe the issue here is that your program is overwriting the created file with its own output, as the
wb = xlsxwriter.Workbook(filename)
statement creates a new file. The conditions under which this might be deleted will depend on when the named temporary file is deleted (technically this happens on close()).
You should think about using mkstemp instead, since you already explicity delete the file you are creating. Overwriting that file, whose name is guaranteed unique and which is not deleted automatically, should be more controllable.

Python 2: AttributeError: 'file' object has no attribute 'strip'

I have a .txt document called new_data.txt. All data in this document separated by dots. I want to open my file inside python, split it and put inside a list.
output = open('new_data.txt', 'a')
output_list = output.strip().split('.')
But I have an error:
AttributeError: 'file' object has no attribute 'strip'
How can I fix this?
Note: My program is on Python 2
First, you want to open the file in read mode (you have it in append mode)
Then you want to read() the file:
output = open('new_data.txt', 'r') # See the r
output_list = output.read().strip().split('.')
This will get the whole content of the file.
Currently you are working with the file object (hence the error).
Update: Seems like this question has received a lot more views since its initial time. When opening files, the with ... as ... structure should be used like so:
with open('new_data.txt', 'r') as output:
output_list = output.read().strip().split('.')
The advantage of this is that there's no need to explicitly close the file, and if an error ever occurs in the control sequence, python will automatically close the file for you (instead of the file being left open after error)

database to csv in django using python

I am writing this small code which is part of my Django application. It is supposed to pick up data from a DB table(MySql) and make a csv file. May be its a very simple error I am getting, but I am not able to resolve it.
Name of the file: write_to_csv.py
import csv
def createCSV():
from django.db import connection, transaction
cursor = connection.cursor()
cursor.execute("select * from avg_max_min;")
csv_writer = csv.writer(open("out.csv", "wt"), delimiter=',')
csv_writer.writerow([i[0] for i in cursor.description]) # write headers
csv_writer.writerows(cursor)
del csv_writer # this will close the CSV file
Error
Exception Value:
'module' object has no attribute 'writer'
Exception Location: C:\Python26\Lib\site-packages\django\bin\report\src\report ..\report\report_view\write_to_csv.py in createCSV, line 6
open's second argument should be wb not wt. Other than that, it looks like you are doing everything right.
If it's still not working, can you update your question with the results of doing dir(csv)? (It's most likely that you have some other module installed in your Python distribution or in the same directory as write_to_csv.py with the same name.)
I'm guessing it's a setup problem (something like this). Make sure you don't have a file named csv.py or some other weirdness that is "hiding" python's csv module.

Categories