Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'ascii': 0.07; 'bits': 0.07; 'bytes.': 0.07; 'character,': 0.07; 'interpreted': 0.07; 'utf-8': 0.07; 'bits.': 0.09; 'defined.': 0.09; 'encode': 0.09; 'subject:script': 0.09; 'subset': 0.09; 'subject:not': 0.11; 'encoding': 0.15; 'file,': 0.15; '#this': 0.16; 'ascii,': 0.16; 'decoding': 0.16; 'encodings': 0.16; 'subject:when': 0.16; 'unicode?': 0.16; 'wrote:': 0.17; 'bytes': 0.17; 'skip:u 30': 0.17; 'unicode': 0.17; 'examples': 0.18; 'windows': 0.19; 'variable': 0.20; 'bit': 0.21; 'error.': 0.21; 'work.': 0.23; 'nearly': 0.23; 'external': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:" 20': 0.26; 'guess': 0.27; 'functions.': 0.27; 'character.': 0.29; 'character': 0.29; 'this.': 0.29; "i'm": 0.29; 'function': 0.30; 'error': 0.30; 'code': 0.31; 'gets': 0.32; 'file': 0.32; 'certain': 0.33; 'to:addr:python-list': 0.33; "can't": 0.34; 'pm,': 0.35; 'sometimes': 0.35; 'there': 0.35; 'but': 0.36; 'characters': 0.36; 'test': 0.36; 'possible': 0.37; 'does': 0.37; 'two': 0.37; 'uses': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'page': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'notice': 0.39; 'called': 0.39; 'received:192.168': 0.40; 'skip:u 10': 0.60; 'most': 0.61; "you'll": 0.62; 'back': 0.62; 'more': 0.63; 'show': 0.63; 'dont': 0.64; 'talking': 0.66; 'received:74.208': 0.71; 'million': 0.72; 'directly.': 0.78; '128,': 0.84; 'different.': 0.84; 'fortunately': 0.84; 'received:74.208.4.194': 0.84; 'subject:running': 0.84; 'device,': 0.91 Date: Tue, 12 Feb 2013 15:51:22 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: UnicodeEncodeError when not running script from IDE References: <650d144e-da3d-4ca7-ad3a-49f44ce9cbaa@googlegroups.com> <0d6d513d-fa12-4d51-a33d-7bb38f1ee6b2@googlegroups.com> <780d353a-de5c-4d04-8f51-11d81802351b@googlegroups.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Provags-ID: V02:K0:TGjUjmqw89+F58wml9OnklNI/W7SX87zIlsrZySs9aJ FsWOBBs0z2KYBcKUsNeoPTmJJj+sxDcJ8ckTRL923ZsiIhhDkY jk1YT9tOBzh0EqIJanc0gpQ6EzPrmUwWSvvgDRQ58FqsmNlj1x TnYfS2Gdk77icvoQYtz0nC9TtQgLJ/A8DKVsTJ8b9gm4T7aoP3 lZwc5gz05vtfgNm/K+DzhJWLZTvgHvYPkMN/nYOZ6aS1rNncKy sD8+8Mk2eMSi+wVueqPA1sCP1pY84obna4NNjxL0odWl8MuuqW HDYyWnpIQIRyFJ23+307tnyrD6c1r5j92CTBB2+ZQKlm7m3cQ= = X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360702303 news.xs4all.nl 6963 [2001:888:2000:d::a6]:49800 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38784 On 02/12/2013 12:12 PM, Magnus Pettersson wrote: >> < snip > >> > #Here kanji = u"私" > baseurl = u"http://www.romajidesu.com/kanji/" > url = baseurl+kanji > savefile([url]) #this test works now. uses: io.open(filepath, "a",encoding="UTF-8") as f: > # This made the fetching of the website work. You don't show the code that actually does the io.open(), nor the url.encode, so I'm not going to guess what you're actually doing. > Why did i have to write url.encode("UTF-8") when url already is unicode? I feel i dont have a good understanding of this. > page = urllib2.urlopen(url.encode("UTF-8")) utf-8 is NOT unicode; they are entirely different. Unicode is conceptually 32 bits per character, and is an internal representation. There are a million or so characters defined. Nearly always when you're talking to an external device, you need bytes. Since you can't cram 32 bits into 8, you have to encode it. Examples of devices would be any file, or the console. Notice that sometimes you can use unicode directly for certain functions. For example, the Windows file name is composed of Unicode characters, so Windows has function calls that accept Unicode directly. But back to 8 bits: One encoding is called ASCII, which is simply the bottommost 7 bits. But of course it gets an error if there are any characters above 127. Other encodings try to pick an 8 bit subset of the million possible characters. Again, if you happen to have a character that's not in that subset, you'll get an error. There are also other encodings which are hard to describe, but fortunately pretty rare these days. Then there's utf-8, which uses a variable length bunch of bytes for each character. It's designed to use the ASCII encoding for characters which are below 128, but uses two or more bytes for all the other characters. So it works out well when most characters happen to be ASCII. Once encoded, a stream of bytes can only be successfully interpreted if you use the same decoding when processing them. -- DaveA