473,796 Members | 2,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"UnicodeErr or: UTF-16 stream does not start with BOM"

10 New Member
I have text file which contain Unicode data (say inp.txt)
I read file using following code:-

Expand|Select|Wrap|Line Numbers
  1. import codecs
  2. infile = codecs.open('C:\\tdata\\inp.txt','r','utf-16',errors='ignore')
  3. data = infile.readlines()
  4.  
If I run above code ... it throws following error :-
Expand|Select|Wrap|Line Numbers
  1. "Traceback (most recent call last):
  2.   File "C:\script\hypen\hyp.py", line 34, in ?
  3.     data = infile.readlines()
  4.   File "C:\Python24\lib\codecs.py", line 489, in readlines
  5.     return self.reader.readlines(sizehint)
  6.   File "C:\Python24\lib\codecs.py", line 404, in readlines
  7.     data = self.read()
  8.   File "C:\Python24\lib\codecs.py", line 293, in read
  9.     newchars, decodedbytes = self.decode(data, self.errors)
  10.   File "C:\Python24\lib\encodings\utf_16.py", line 49, in decode
  11.     raise UnicodeError,"UTF-16 stream does not start with BOM"
  12. UnicodeError: UTF-16 stream does not start with BOM"
But if I do create a new file (I did in Notepad on Win XP) and copy paste content of 'inp.txt' in it and save it as text file (choosing Unicode encoding which same as of inp.txt). Now with same above code reading this new file, it works absolutely fine. this seems weird... is notepad created file added some own magic chars :)

Can anyone help me regarding this , what can be the issue here ? . Why creating a new file and saving contents in it worked FINE while original file still throws error. (I have got such 15 localized files from clients on which some processing as to be done, I want to avoid manually copy/paste rework). Any help appreciated...


Thanks,
anil
Jan 6 '09 #1
1 35080
bvdet
2,851 Recognized Expert Moderator Specialist
I found information on this link helpful. Since you know your encoding is "UTF-16", you may be able to use string method decode() to read your data. Notepad adds the BOM based on the encoding selected.
Jan 6 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
5496
by: nico | last post by:
In my python scripts, I use a lot of accented characters as I work in french. In order to do this, I put the line # -*- coding: UTF-8 -*- at the beginning of the script file. Then, when I need to store accented characters in a string, I used to prefix the literal string with 'u', like this: mystring = u"prénom" But if I understand well, prefixing a unicode string literal with 'u'
3
3320
by: Mike Kennedy | last post by:
I have an XML Snippet <?xml version="1.0" encoding="UTF-8"?> and when I convert the entire xml file to a DOM and then generate a new file from the DOM, results in <?xml version="1.0"?>. Any thoughts? My sample code snippet is: DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(in.xml); FileWriter file = new FileWriter(out.xml);
4
2415
by: peter | last post by:
When I transform a dataset to a xml file by applying a xslt file, why by default, a "encoding="utf-8" ?" is added to the output file?How to get rid of it? Thanks
3
2545
by: Christian Nunciato | last post by:
Hi all: I've read through the various related posts in this forum, but without any success as yet. I've got an ASP.NET application built in VS.NET 2003, and I'm trying to get the Armenian Unicode characters I type into the textboxes of my ASPX Web pages (via IE6) to post back to the server and get saved in my SQL database as Armenian Unicode characters. Not having much luck, though -- all I get back are question marks.
12
7364
by: Mark | last post by:
In our web.config, we have changed the first line below to look like the second: OLD: <globalization requestEncoding="utf-8" responseEncoding="utf-8" /> NEW: <globalization requestEncoding="ISO-8859-1" responseEncoding="ISO-8859-1" /> What characters are we excluding from working properly and/or what problems might we encounter by making this change to our site? Practical examples would be appreciated.
1
376
by: Daniel | last post by:
how to parse <?xml version="1.0" encoding="UTF-8"?> with xpath? is it possible?
1
2597
by: NevilleDNZ | last post by:
Hi, Apologies first as I am not a unicode expert.... indeed I the details probably totally elude me. Not withstanding: how can I convert a binary string containing UTF-8 binary into a python unicode string? cutdown example: $ cat ./uc.py #!/usr/bin/env python imported="\304\246\311\231\316\257\316\271\303\222
6
7551
by: Flavio | last post by:
Hi I am havin a problem with urllib2.urlopen. I get this error when I try to pass a unicode to it. raise UnicodeError, "label too long" is this problem avoidable? no browser or programs such as wget seem to have a problem with these strings.
2
2599
by: ashwinij | last post by:
Hello The steps which i am doing in my program 1) I am having an xml file. 2) I am performing some updations in the file using XQueryUtil class from nux package. 3)After that i am performing Serialisation ( nu.xom.Serializer ) and storing in a file. 4)This Serialised file is being appended in a separate file. 5) Steps 3) and 4) are performed for som n no. of times The problem is whenever i serialised i get this line "<?xml...
3
5242
by: robert.szczepanski | last post by:
Hi Everybody Why the following script return NULL? this is the following: <?php var_dump ( setlocale(LC_ALL, "pl") ); ?>
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9531
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10237
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10187
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7553
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5446
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.