I have this csv file dl.dropboxusercontent.com/s/tb4yc3lm3gg3j22/out.csv and I am trying to replace the last column with the read method below.
In my input.csv i have to replace the last filed with some calculation. I trying as below but somehow i am getting ValueError: I/O operation on a closed file. Can you help me point out the mistake. As i am trying to open input file and write everything including the new value to out.csv file which doesnt exist initially but should be constructed on the fly.
def read():
for file in os.listdir("./"):
if file.endswith('.csv'):
fNameFull = os.path.basename(file)
inputFileName = os.path.splitext(file)[0]
with open(fNameFull, "rb") as infile, open('out.csv', "wb") as outfile:
r = csv.DictReader(infile)
w = csv.DictWriter(outfile, r.fieldnames)
w.writeheader()
for row in r:
if not row["col_last"].strip():
row["col_last"] = "calc_value"
w.writerow(row)
if __name__ == '__main__':
main()
read()
Error:
Traceback (most recent call last):
File "C:\Users\hp\Desktop\final\text\finalmod.py", line 50, in <module>
read()
File "C:\Users\hp\Desktop\final\text\finalmod.py", line 39, in read
w.writeheader()
File "C:\Python27\lib\csv.py", line 137, in writeheader
self.writerow(header)
File "C:\Python27\lib\csv.py", line 148, in writerow
return self.writer.writerow(self._dict_to_list(rowdict))
ValueError: I/O operation on closed file
Related
I've just rebuilt my Raspberry Pi and hence installed the latest version of the Dropbox API and now my program doesn't work. I think this is due to point 1 in these breaking changes: https://github.com/dropbox/dropbox-sdk-python/releases/tag/v7.1.0. I'm sure this question from SO (Dropbox API v2 - trying to upload file with files_upload() - throws TypeError) solves my problem... but as a newbie, I can't figure out how to actually implement it - and anyway, I'm already using f.read()... can anyone help?
This is my code:
def DropboxUpload(file):
sourcefile = "/home/pi/Documents/iot_pm2/dropbox_transfer/" + filename
targetfile = "/" + filename
dbx = dropbox.Dropbox(cfg.dropboxtoken)
f = open(sourcefile, "r")
filecontents = f.read()
try:
dbx.files_upload(filecontents, targetfile, mode=dropbox.files.WriteMode.overwrite)
except dropbox.exceptions.ApiError as err:
print(err)
f.close()
And this is the error:
Traceback (most recent call last):
File "/home/pi/Documents/iot_pm2/dropbox_uploader.py", line 20, in <module>
DropboxUpload(filename)
File "/home/pi/Documents/iot_pm2/dropbox_uploader.py", line 12, in DropboxUpload
dbx.files_upload(filecontents, targetfile, mode=dropbox.files.WriteMode.overwrite)
File "/usr/local/lib/python3.5/dist-packages/dropbox/base.py", line 2125, in files_upload
f,
File "/usr/local/lib/python3.5/dist-packages/dropbox/dropbox.py", line 272, in request
timeout=timeout)
File "/usr/local/lib/python3.5/dist-packages/dropbox/dropbox.py", line 363, in request_json_string_with_retry
timeout=timeout)
File "/usr/local/lib/python3.5/dist-packages/dropbox/dropbox.py", line 407, in request_json_string
type(request_binary))
TypeError: expected request_binary as binary type, got <class 'str'>
Thanks in advance.
You need to supply bytes, but you're supplying str.
You can get bytes by changing the file mode to binary. I.e., instead of:
f = open(sourcefile, "r")
do:
f = open(sourcefile, "rb")
I am trying to download a hdf file and read it python as follows
from pyhdf import SD
file = open("temp.hdf", 'w')
ftp.retrbinary('RETR '+ filename, file.write)
file.close()
hdf=SD.SD('temp.hdf')
It works but soon after the I am getting the following Error:
Traceback (most recent call last):
File "<ipython-input-46-55805a9d569b>", line 6, in <module>
hdf=SD.SD('temp.hdf')
File "/usr/local/lib/python2.7/dist-packages/pyhdf/SD.py", line 1444, in __init__
_checkErr('SD', id, "cannot open %s" % path)
File "/usr/local/lib/python2.7/dist-packages/pyhdf/error.py", line 23, in _checkErr
raise HDF4Error(err)
HDF4Error: SD (59): HDF Internal error
You need to open the output file in binary mode:
file = open("temp.hdf", 'wb') # was 'w'
Better would be to use with to automatically close the file:
with open("temp.hdf", 'wb') as out:
ftp.retrbinary('RETR '+ filename, out.write)
Dears,
I'm creating a script python to mass upload files in Plone site, the installation is UnifiedInstaller Plone 4.3.10.
This script read a txt, and this txt have separation with semicolon, the error appear when set up a file in a new created item.
Bellow the Script.
from zope.site.hooks import setSite
from plone.namedfile.file import NamedBlobFile
from plone import api
import transaction
import csv
portal = app['Plone']
setSite(portal)
container = portal['PROCESSOS']
with open('CARGA/C008_0002.txt', 'rb') as csvfile:
reader = csv.DictReader(csvfile, delimiter=';', quotechar='|')
for row in reader:
pdf_id = 'P'+str(row['IMAGEM']).strip('Pasta Geral\\ ')
file_obj = api.content.create(
container, 'File',
title=str(row['INTERESSADO']),
id=pdf_id,
description=str(row['CNPJ / CPF'])+' '+str(row['ASSUNTO']),
safe_id=True
)
pdf_path = 'INMEQ/'+str(row['IMAGEM']).replace("\\", "/")
print(pdf_path)
file_obj.file = NamedBlobFile(
data=open(pdf_path, 'r').read(),
contentType='application/pdf',
filename=str(file_obj.id),
)
print('Created: '+row['NDOPROCESSO']+'.')
transaction.commit()
When the script will set up a file the error "WrongType" appear. See verbose bellow.
Traceback (most recent call last):
File "<console>", line 18, in <module>
File "/home/jaf/plone4310/buildout-cache/eggs/plone.namedfile-3.0.9-py2.7.egg/plone/namedfile/file.py", line 384, in __init__
self.filename = filename
File "/home/jaf/plone4310/buildout-cache/eggs/zope.schema-4.2.2-py2.7.egg/zope/schema/fieldproperty.py", line 52, in __set__
field.validate(value)
File "/home/jaf/plone4310/buildout-cache/eggs/zope.schema-4.2.2-py2.7.egg/zope/schema/_bootstrapfields.py", line 182, in validate
self._validate(value)
File "/home/jaf/plone4310/buildout-cache/eggs/zope.schema-4.2.2-py2.7.egg/zope/schema/_bootstrapfields.py", line 309, in _validate
super(MinMaxLen, self)._validate(value)
File "/home/jaf/plone4310/buildout-cache/eggs/zope.schema-4.2.2-py2.7.egg/zope/schema/_bootstrapfields.py", line 209, in _validate
raise WrongType(value, self._type, self.__name__)
WrongType: ('processo-al-1.pdf', <type 'unicode'>, 'filename')
Thanks for you attention!
--
Juliano Araújo
You need to pass the filename as unicode.
file_obj.file = NamedBlobFile(
data=open(pdf_path, 'r').read(),
contentType='application/pdf',
filename=unicode(file_obj.id), # needs to be unicode
)
More Info in the plone.namedfile docu --> https://github.com/plone/plone.namedfile/blob/36014d67c3befacfe3a058f1d3d99a6a4352a31f/plone/namedfile/usage.rst
I have this snippet of Python code:
import csv
def analyse(csvFileToRead, csvFileToWrite):
# open file to read
openedCsvFileToRead = open(csvFileToRead)
reader = csv.reader(openedCsvFileToRead)
# open file to write
openedCsvFileToWrite = open(csvFileToWrite)
writer = csv.writer(openedCsvFileToWrite)
for row in reader:
date = row[8]
if date[0] == "5":
writer.writerow(row)
# close file
openedCsvFileToRead.close()
openedCsvFileToWrite.close()
if __name__ == "__main__":
analyse("mydata.csv", "mynewdata.csv")
when run using Python 3.4 I get the following error message:
Traceback (most recent call last):
File "main.py", line 40, in <module>
analyse("mydata.csv", "mynewdata.csv")
File "main.py", line 25, in analyse
writer.writerow(row)
io.UnsupportedOperation: not writable
What Am I doing wrong?
I'm on Windows 7 64bit.
You have to open the file in write mode:
openedCSvFileToWrite = open(csvFileToWrite, "w")
Note that in Python 2.x, the docs always use 'wb', rather than 'w'.
Here's the problem - I'm trying to pickle, and then unpickle hiscores. When I use pickle.load, Python seems to think that I'm trying to load a file called 'Pickle' that I have. Here's the code:
def recieve_hiscores():
hiscores_file = open("hiscores_file.dat", "rb")
for i in hiscores_file:
hiscores = pickle.load(hiscores_file)
hiscores = str(hiscores)
print(hiscores)
Here's the pickling code:
def send_hiscores(score):
hiscores_file = open("hiscores_file.dat", "ab")
pickle.dump(score, hiscores_file)
hiscores_file.close()
And here's the error message:
Traceback (most recent call last):
File "C:\Python31\My Updated Trivia Challenge.py", line 106, in <module>
main()
File "C:\Python31\My Updated Trivia Challenge.py", line 104, in main
recieve_hiscores()
File "C:\Python31\My Updated Trivia Challenge.py", line 56, in recieve_hiscores
hiscores = pickle.load(hiscores_file)
File "C:\Python31\lib\pickle.py", line 1365, in load
encoding=encoding, errors=errors).load()
EOFError
Don't worry if there's any other mistakes, I'm still learning, but I can't work this out.
When you iterate over the file, you get newline separated lines. This is NOT how you get a series of pickles. The end of file error is raised because the first line has a partial pickle.
Try this:
def recieve_hiscores():
highscores = []
with open("hiscores_file.dat", "rb") as hiscores_file:
try:
while True:
hiscore = pickle.load(hiscores_file)
hiscore = str(hiscore)
print(hiscore)
highscores.append(hiscore)
except EOFError:
pass
return highscores