EOF error when using "rb" - python-3.x

here is my code:
authors_file_handler = open(authors_file,'r')
authors = pickle.load(authors_file_handler)
authors_file_handler.close()
authors_file is a .pkl file and I have confirmed info that it is not empty.
On using 'rb' or 'r+b' in place of 'r', I get an error:
File "../tools\email_preprocess.py", line 33, in preprocess
authors = pickle.load(authors_file_handler)
EOFError: Ran out of input
PLease advise on how to handle this error.

Related

Getting a OSERROR: [Errno 121] Remote I/O error

I am facing problems with my code that has been working perfectly fine and ran everything that I needed it to. This happens from time to time but this time I don't know what my problem is. I recently tried to place a sampling frequency so I can control how many times my data is running in a second but since I made those changes I had nothing but errors so I deleted the changes that I made and now I have errors although I am using the original code that I was using before hand.
My electrical connection is perfect so this is not the issue. I also am not getting any errors in the terminal while using i2cget -y 1
This is my python code (also using INA219 sensor):
#Importing libraries
import csv
from ina219 import INA219
from ina219 import DeviceRangeError
SHUNT_OHMS = 0.1
read_ina = INA219(SHUNT_OHMS)
read_ina.configure()
def read_all():
data = {}
data['Bus Voltage'] = read_ina.voltage()
data['Bus Current'] = read_ina.current()
data['Power'] = read_ina.power()
data['Shunt Voltage'] = read_ina.shunt_voltage()
return data
with open('SensorData.csv', 'w') as f:
data = read_all()
writer = csv.DictWriter(f,
fieldnames = list (data.keys()))
writer.writeheader()
exit = False
while not exit:
try:
writer.writerow(data)
data = read_all()
except KeyboardInterrupt:
exit = True
It is supposed to create a csv file that shows the voltage and all of that in a loop (in the csv file). The code is pretty straightforward. Can anyone help me fix this issue?
This is the error that I keep facing:
Traceback (most recent call last):
File "/home/pi/Downloads/scripts/Assignment2 CreateCSV/SensorData.py", line 40, in <module>
data = read_all()
File "/home/pi/Downloads/scripts/Assignment2 CreateCSV/SensorData.py", line 20, in read_all
data['Bus Voltage'] = read_ina.voltage()
File "/usr/local/lib/python3.5/dist-packages/ina219.py", line 180, in voltage
value = self._voltage_register()
File "/usr/local/lib/python3.5/dist-packages/ina219.py", line 363, in _voltage_register
register_value = self._read_voltage_register()
File "/usr/local/lib/python3.5/dist-packages/ina219.py", line 367, in _read_voltage_register
return self.__read_register(self.__REG_BUSVOLTAGE)
File "/usr/local/lib/python3.5/dist-packages/ina219.py", line 394, in __read_register
register_value = self._i2c.readU16BE(register)
File "/usr/local/lib/python3.5/dist-packages/Adafruit_GPIO/I2C.py", line 190, in readU16BE
return self.readU16(register, little_endian=False)
File "/usr/local/lib/python3.5/dist-packages/Adafruit_GPIO/I2C.py", line 164, in readU16
result = self._bus.read_word_data(self._address,register) & 0xFFFF
File "/usr/local/lib/python3.5/dist-packages/Adafruit_PureIO/smbus.py", line 226, in read_word_data
ioctl(self._device.fileno(), I2C_RDWR, request)
OSError: [Errno 121] Remote I/O error

Not able to parse csv file from pandas

I am writing python script in which i am generating two different csv files and then reading these file by using pandas. I am able to read file1 with pandas but getting error while reading file2 which in same format(same column name) as file1 but different/same values. Please find the below error that i am getting and sample code that i am using.
Error:
Traceback (most recent call last):
File "MSReport.py", line 168, in <module>
fail = pd.read_csv('/home/cisapp/msLogFailure.csv', sep=',')
File "/home/cisapp/.local/lib/python3.6/site-packages/pandas/io/parsers.py", line 676, in parser_f
return _read(filepath_or_buffer, kwds)
File "/home/cisapp/.local/lib/python3.6/site-packages/pandas/io/parsers.py", line 448, in _read
parser = TextFileReader(fp_or_buf, **kwds)
File "/home/cisapp/.local/lib/python3.6/site-packages/pandas/io/parsers.py", line 880, in __init__
self._make_engine(self.engine)
File "/home/cisapp/.local/lib/python3.6/site-packages/pandas/io/parsers.py", line 1114, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "/home/cisapp/.local/lib/python3.6/site-packages/pandas/io/parsers.py", line 1891, in __init__
self._reader = parsers.TextReader(src, **kwds)
File "pandas/_libs/parsers.pyx", line 532, in pandas._libs.parsers.TextReader.__cinit__
pandas.errors.EmptyDataError: No columns to parse from file
Code:
df = pd.read_csv(BASE_LOCATION+'/msLog_Success.csv', engine='python')
f_output = df.groupby('MSISDN').last()
#print(df)
print(f_output)
fail = pd.read_csv(BASE_LOCATION+'/msLogFailure.csv', engine='python')
fail = fail['MSISDN']
fail = fail.tolist()
for i in fail:
succ = f_output[f_output.MSISDN != i]
In above sample code there is no error while reading file df = pd.read_csv(BASE_LOCATION+'/msLog_Success.csv', engine='python') but while reading file fail = pd.read_csv(BASE_LOCATION+'/msLogFailure.csv', engine='python') i am facing the error as mentioned above. Please help to resolve.
Note: I am running code by using python3.
I faced the same problem and resolved. So you can check using below idea.
Check the delimitator and mention like below examples
pd.read_csv(BASE_LOCATION+'/msLog_Success.csv', encoding='utf-16', sep='\t')
pd.read_csv(BASE_LOCATION+'/msLog_Success.csv', delim_whitespace=True)
You can also add 'r' before file path.
Otherwise share the file image
Your sample of msLogFailure file looks OK - 6 column names and 6 data fields.
I looked for posts concerning just this error message and I found an advice to:
read the input file into a string variable,
read_csv from this string, e.g. pd.read_csv(io.StringIO(txt),...).
Maybe this will help.

Using python to convert xls files to csv and saving them into one file

I am trying to convert multiple .xls files into one .csv file so i can use in a later script.
I found most of this code in other Stackoverflow pages but when trying to run the code below i get the following error:
Error:
Traceback (most recent call last):
File "script_base_teste.py", line 13, in <module>
wr.writerow(sh.row_values(rownum))
TypeError: a bytes-like object is required, not 'str'
Code:
import xlrd
import csv
for num in range(1,4):
wb = xlrd.open_workbook("file"+str(num)+".xls")
sh = wb.sheet_by_name("Sheet 1")
your_csv_file = open("file"+str(num)+".csv", "wb")
wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)
for rownum in range(0,sh.nrows):
wr.writerow(sh.row_values(rownum))
your_csv_file.close()
fout=open("result.csv","a")
first file:
for line in open("file1.csv"):
fout.write(line)
now the rest:
for num in range(2,3):
f = open("file"+str(num)+".csv")
f.next() # skip the header
for line in f:
fout.write(line)
f.close()
fout.close()
I'm using Python3 and I got most of the code from this and this
You opened the file in binary mode
your_csv_file = open("file"+str(num)+".csv", "wb")
The "wb" stands for "write binary".
You can only read/write binary data in this mode.
Since you want to write using strings, you can open it in write mode, using "w" instead of "wb".
your_csv_file = open("file"+str(num)+".csv", "w")

Contradicting Errors?

So I'm trying to edit a csv file by writing to a temporary file and eventually replacing the original with the temp file. I'm going to have to edit the csv file multiple times so I need to be able to reference it. I've never used the NamedTemporaryFile command before and I'm running into a lot of difficulties. The most persistent problem I'm having is writing over the edited lines.
This part goes through and writes over rows unless specific values are in a specific column and then it just passes over.
I have this:
office = 3
temp = tempfile.NamedTemporaryFile(delete=False)
with open(inFile, "rb") as oi, temp:
r = csv.reader(oi)
w = csv.writer(temp)
for row in r:
if row[office] == "R00" or row[office] == "ALC" or row[office] == "RMS":
pass
else:
w.writerow(row)
and I get this error:
Traceback (most recent call last):
File "H:\jcatoe\Practice Python\pract.py", line 86, in <module>
cleanOfficeCol()
File "H:\jcatoe\Practice Python\pract.py", line 63, in cleanOfficeCol
for row in r:
_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
So I searched for that error and the general consensus was that "rb" needs to be "rt" so I tried that and got this error:
Traceback (most recent call last):
File "H:\jcatoe\Practice Python\pract.py", line 86, in <module>
cleanOfficeCol()
File "H:\jcatoe\Practice Python\pract.py", line 67, in cleanOfficeCol
w.writerow(row)
File "C:\Users\jcatoe\AppData\Local\Programs\Python\Python35-32\lib\tempfile.py", line 483, in func_wrapper
return func(*args, **kwargs)
TypeError: a bytes-like object is required, not 'str'
I'm confused because the errors seem to be saying to do the opposite thing.
If you read the tempfile docs you'll see that by default it's opening the file in 'w+b' mode. If you take a closer look at your errors, you'll see that you're getting one on read, and one on write. What you need to be doing is making sure that you're opening your input and output file in the same mode.
You can do it like this:
import csv
import tempfile
office = 3
temp = tempfile.NamedTemporaryFile(delete=False)
with open(inFile, 'r') as oi, tempfile.NamedTemporaryFile(delete=False, mode='w') as temp:
reader = csv.reader(oi)
writer = csv.writer(temp)
for row in reader:
if row[office] == "R00" or row[office] == "ALC" or row[office] == "RMS":
pass
else:
writer.writerow(row)

UnicodeDecodeError Python 3.5.1 Email Script

I am attempting to send an email + attachment to an SMS gateway email. However I currently am getting a Unicode Decode: Error'Charmap' codec can't Decode Byte 0x8d in position 60
I'm not sure how to go about fixing this and would be interested in your advice. Bellow is my code and the Full Error.
import smtplib, os
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
msg = MIMEMultipart()
msg['Subject'] = 'Cuteness'
msg['From'] = 'sample#outlook.com'
msg['To'] = '111111111#messaging.sprintpcs.com'
msg.preamble = "Would you pet me? I'd Pet me so hard..."
here = os.getcwd()
file = open('cutecat.png')#the png shares directory with actual script
for here in file: #Error appears to be in this section
with open(file, 'rb') as fp:
img = MIMImage(fp.read())
msg.attach(img)
s = smtplib.SMTP('Localhost')
s.send_message(msg)
s.quit()
""" Traceback (most recent call last):
File "C:\Users\Thomas\Desktop\Test_Box\msgr.py", line 16, in <module>
for here in file:
File "C:\Users\Thomas\AppData\Local\Programs\Python\Python35-32\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 60: character maps to <undefined>"""
You're trying to open the file twice. First you have:
file = open('cutecat.png')
The default mode to open files is to read them in text mode. That is generally not what you want to do with a binary file like a PNG file.
And then you do:
for here in file:
with open(file, 'rb') as fp:
img = MIMImage(fp.read())
msg.attach(img)
You get an exception in the first line because Python is trying to decode the contents of a binary file as text and fails. The chances of this happening are quite high. It is unlikely that a binary file is also a valid text file in your standard encoding.
But even if that would have worked, for every line in the file you try to open the file again? This makes no sense!
Were you just copy/pasting from the examples, especially the third one? You should note that this example is incomplete. The variable pngfiles used in that example (and which should be a sequence of file names) is not defined.
Try this instead:
with open('cutecat.png', 'rb') as fp:
img = MIMImage(fp.read())
msg.attach(img)
Or if you want to include multiple files:
pngfiles = ('cutecat.png', 'kitten.png')
for filename in pngfiles:
with open(filename, 'rb') as fp:
img = MIMImage(fp.read())
msg.attach(img)

Resources