UTF-8 Encoding Error

subhabangalore at gmail.com subhabangalore at gmail.com
Thu Dec 29 17:19:57 EST 2016


On Friday, December 30, 2016 at 3:35:56 AM UTC+5:30, subhaba... at gmail.com wrote:
> On Monday, December 26, 2016 at 3:37:37 AM UTC+5:30, Gonzalo V wrote:
> > Try utf-8-sig
> > El 25 dic. 2016 2:57 AM, "Grady Martin" <> escribió:
> > 
> > > On 2016年12月22日 22時38分,  wrote:
> > >
> > >> I am getting the error:
> > >> UnicodeDecodeError: 'utf8' codec can't decode byte 0x96 in position 15:
> > >> invalid start byte
> > >>
> > >
> > > The following is a reflex of mine, whenever I encounter Python 2 Unicode
> > > errors:
> > >
> > > import sys
> > > reload(sys)
> > > sys.setdefaultencoding('utf8')
> > >
> > > A relevant Stack Exchange thread awaits you here:
> > >
> > > http://stackoverflow.com/a/21190382/2230956
> > > --
> > > https://mail.python.org/mailman/listinfo/python-list
> > >
> 
> Thank you for your kind time and answers. 
> 
> I tried to open one file in default ASCII format in MS-Windows 7. 
> txtf=open("/python27/TestFiles/small_file_1.txt","r").read()
> I could write them in UTF-8 using 
> cd1=codecs.open("/python27/TestFiles/file1.pos","w", "utf-8-sig")
> cd1.write(txtf)
> 
> Here, I was getting an error as,
> UnicodeDecodeError: 'ascii' codec can't decode byte 0x96 in position 150: ordinal not in range(128)
> 
> Then I used,
> >>> import sys
> >>> reload(sys)
> >>> sys.setdefaultencoding('utf8')
> 
> and then wrote 
> >>> cd1.write(txtf)
> it went fine. 
> 
> Now in my actual problem I am writing it bit differently: 
> 
> with open('textfile.txt') as f:
>         for i, g in enumerate(grouper(n, f, fillvalue=''), 1):
>             with open('/Python27/TestFiles/small_filing_{0}.pos'.format(i * n), 'w') as fout:
>                 fout.writelines(g)
> 
> I am trying to fix this. 
> 
> If you may kindly suggest.

The grouper method is:
def grouper(n, iterable, fillvalue=None):
        "Collect data into fixed-length chunks or blocks"
        args = [iter(iterable)] * n
        return izip_longest(fillvalue=fillvalue, *args)
n = 3



More information about the Python-list mailing list