Uncatchable Null Byte csv.Error Exception - python-3.x

I'm trying to catch a Null Byte Exception in the last line of a CSV file:
def Catch(csv_filename):
with open(csv_filename,'r+') as File:
File_reader = csv.reader(File,delimiter="\t",dialect='excel-tab')
a = []
for row in File_reader:
try:
a.append(row)
except csv.Error:
return "Error"
Catch("/../DataLogger.csv")
but an _csv.Error is raised:
Traceback (most recent call last):
File "/../test.py", line 21, in <module>
Catch("/../DataLogger.csv")
File "/../test.py", line 13, in Catch
for row in File_reader:
_csv.Error: line contains NULL byte
I don't get why the exception is not catched with the function.
I'm using python 3.4

that's because the exception occurs as soon as your code reaches the for statement.
No exception can happen in the a.append line, the csv module does its job in the iteration of the for loop.
Once you know that, the fix is trivial:
try:
for row in File_reader:
a.append(row)
except csv.Error:
return "Error"
note that one could be tempted to use direct conversion to list: a = list(File_reader) but since the exception would take place in the list conversion, a wouldn't be filled, which would be a nuisance if the start of the file contains useful data you want to read (but since you're returning an error string, it doesn't seem to matter here)

def Catch(csv_filename):
with open(csv_filename,'r+') as File:
try:
File_reader = csv.reader(File,delimiter="\t",dialect='excel-tab')
a = []
for row in File_reader:
a.append(row)
except csv.Error
return "Error"
Catch("/../DataLogger.csv")
The whole parsing has to be inside Try/catch, instead of only a.append.

Related

Saving tkinter variables to txt file error

I am trying to make my program save some tkinter String variables to a txt files.
Here is the code:
def saveFile():
file = filedialog.asksaveasfile(mode='w')
if file != None:
file.write(seat1, seat2, seat3, seat4, seat5)
file.close()
Then I get an error when I try to save the file:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Thonny\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "E:\Teacher Plan\seat-plan.py", line 64, in saveFile
file.write(seat1, seat2, seat3, seat4, seat5)
TypeError: write() takes exactly one argument (5 given)
Any ideas?
Make ALL your variables into 1 variable seat[0], seat[1], seat[2], seat[3], seat[4] and then save (seat)
I've used this in one of my projects and it works fine
First you define your main varibale at the start
ess=[[],[],[],[],[],[]]
Then you make your work with variables, when you're done you save (or append) then into the single variable and save to file
ess[0].append(ess_e), ess[1].append(essv), ess[2].append(essp), ess[3].append(essq), ess[4].append(ess_s), ess[5].append(ess_d)
file = open("relais.txt", "w")
file.write(repr(ess) + "\n")
file.close()

pyspark streaming: failing to execute rdd.count() on workers

I have a pyspark streaming job doing something along these lines:
def printrddcount(rdd):
c = rdd.count()
print("{1}: Received an RDD of {0} rows".format("CANNOTCOUNT", datetime.now().isoformat()) )
and then:
...
stream.foreachRDD(printrddcount)
From what I get, the printrdd function will be executed within the workers
And, yes, I know it's a bad idea to do a print() within the worker. But that's not the point.
I'm pretty sure this very code was working until very recently.
(and, it looked differently, because the content of 'c' was actually printed in the print statement, rather than just collected, and then thrown away...)
But now, it seems that (all of a sudden?), then rdd.count() has stopped working ans is making my worker process die saying:
UnpicklingError: NEWOBJ class argument has NULL tp_new
full (well, python only) stacktrace:
Caused by: org.apache.spark.api.python.PythonException: Traceback (most recent call last):
File "/usr/hdp/current/spark2-client/python/lib/pyspark.zip/pyspark/worker.py", line 163, in main
func, profiler, deserializer, serializer = read_command(pickleSer, infile)
File "/usr/hdp/current/spark2-client/python/lib/pyspark.zip/pyspark/worker.py", line 54, in read_command
command = serializer._read_with_length(file)
File "/usr/hdp/current/spark2-client/python/lib/pyspark.zip/pyspark/serializers.py", line 169, in _read_with_length
return self.loads(obj)
File "/usr/hdp/current/spark2-client/python/lib/pyspark.zip/pyspark/serializers.py", line 454, in loads
return pickle.loads(obj)
UnpicklingError: NEWOBJ class argument has NULL tp_new
The line where it fails is, indeed, the one saying rdd.count()
Any idea why rdd.count() would fail?
If something is supposed to be serialized, it should be the rdd, right?
Ok. I investigated a bit further.
There's nothing wrong with rdd.count()
Only thing wrong is that there is another transformation along the pipe that somehow 'corrupts' (Closes? Invalidates? Something along those lines) the rdd.
So, when it gets to the printrddcount function it cannot be serialized any more and gives the error.
The issue is within a code that looks like:
...
log = logging.getLogger(__name__)
...
def parse(parse_function):
def parse_function_wrapper(event):
try:
log.info("parsing")
new_event = parse_function(event)
except ParsingFailedException as e:
pass
return new_event
return parse_function_wrapper
and then:
stream = stream.map(parse(parse_event))
Now, the log.info (tried a lot of variations, in the beginning logging was within an exception handler) is the one creating the issue.
Which leads me to say that, most probably, it is the logger object that cannot be serialized, for some reason.
Closing this thread myself as it has actually nothing to do with rdd serialization; and most probably not even with pyspark even.

'If object is None' not behaving as expected

Writing an analytics script for facebook chats with python. The library I'm using (fbchat) sometimes returns None type objects in place of messages. I thought I could catch this with a simple
if message is None:
continue
in my loop through my message list. However, I'm still getting exceptions due to the object being None type. What am I doing wrong here?
Code snippet:
for message in messages:
#sometimes the fbchat library doesn't return a message
if message is None:
continue
#strip the line endings to not mess up the csv
message.text.replace('\n', ' ')
Exact exception:
Traceback (most recent call last):
File "collect.py", line 58, in <module>
message.text.replace('\n', ' ')
AttributeError: 'NoneType' object has no attribute 'replace'
Checking for message.text is None resolves the issue and is what I should have done in the first place.

Getting Python error, "TypeError: 'NoneType' object is not callable" SOMETIMES

Not very new to programming or to python but incredibly green to using pyunit. Need to use it for my new job and I keep getting this error but only sometimes when it is run. My code below.
import unittest
from nose_parameterized import parameterized
from CheckFromFile import listFileCheck, RepresentsFloat
testParams = listFileCheck()
class TestSequence(unittest.TestCase):
#parameterized.expand(testParams)
def test_sequence(self, name, a, b):
if RepresentsFloat(a):
self.assertAlmostEqual(a,b,2)
else:
self.assertEqual(a,b)
if __name__ == '__main__':
unittest.main()
What is happening here is that my test case is pulling a method listFileCheck from another class. What it does is it reads values from the serial port communicating with the control board and compares them with a calibration file. It puts the control board values in an MD array along with the calibration file values. These values can be either str, int, or float.
I used the test case to compare the values to one another however I keep getting this error but only sometimes. After every 3rd or so run it fails with this error.
Error
Traceback (most recent call last):
File "C:\Python34\lib\unittest\case.py", line 57, in testPartExecutor
yield
File "C:\Python34\lib\unittest\case.py", line 574, in run
testMethod()
TypeError: 'NoneType' object is not callable
Process finished with exit code 0
Anyone know why I might be getting this error on occasion?

NameError and ValueError in Python

Why is python shell throwing a NameError where as windows console a ValueError?
def PrintArgs(*arg):
list = ['1','2']
for i in arg:
try:
print(list[int(i)])
except ValueError:
print('Please enter integer value')
except NameError:
print('Name Error')
if __name__ == '__main__':
PrintArgs(*sys.argv[1:])
Providing the following arguments to Windows Console gives this output:
Here is how I call the code in windows console:
C:\>C:\Python34\python C:\Users\User\Documents\PYTest\Test.py 0 a
1
Please enter integer value
Providing the following arguments to Python Shell does not display the cusom error for NameError as mentioned in the code above, but mentions the following error:
PrintArgs(0,a)
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
PrintArgs(0,a)
NameError: name 'a' is not defined
In the code example you've provided you define a list i, then you iterate over a collection called list you never initiated, and assign the values in this list to i, thus dropping the original value. I guess you only provided a part of your code, please provide a minimum working example.
If I try to reproduce your problem, I only get a type error, for iterating over a list which is not initialized.

Resources