Newbie question: Unicode hiccup on reading file i just wrote
hi all, i have a newbie problem arising from writing-then-reading a
unicode file, and i can't work out what syntax i need to read it in.
the syntax i'm using now (just using quick hack tmp files):
BEGIN
f=codecs.open("tt.xml","r","utf8")
fwrap=codecs.EncodedFile(f,"ascii","utf8")
try:
ss=u''
ss=fwrap.read()
print ss
## rrr=xml.dom.minidom.parseString(f.read()) # originally
finally:
f.close()
END
barfs with this error:
BEGIN
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in
position 5092: ordinal not in range(128)
END
any ideas?
--
Context (if interested):
had a look at the blogger api, downloaded the "15 most recent posts"
into a miniDOM document, then decided to learn how to traverse the xml
object in python. getting annoyed with the time taken to reconnect each
time i played with a new syntax, i wrote the xml object to a file. that
barfed with a similar sort of encoding error. sure enough, there in the
debug coming back from blogger: "charset=utf-8". my python book said i
needed to switch from "open/print" to "codecs.open/write", so i did this:
BEGIN
# get xml doct (from blogger: atom format)
rrr=xml.dom.minidom.Document()
conn.request("GET","/atom/1234",None,headers)
response=conn.getresponse()
rrr=xml.dom.minidom.parseString(response.read())
print rrr
# dump to disk
import codecs
f=codecs.open("ttt.xml","w","utf8")
try:
##print >> f, rrr.toxml()
f.write(rrr.toxml())
finally:
f.close()
END
this works fine and the resulting file looks like good xml to the naked eye.
oh and i have tried both "utf8" and "utf-8" as the en/decoding tokens --
no change.
ditto with explicitly initialising "ss" as unicode: same error as before
when it was not explicitly initialised at all.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Newbie question: Unicode hiccup on reading file i just wrote
Fredrik Lundh wrote:
> Diez B. Roggisch wrote:
>>Just don't do any fancy encoding stuff at all, a simple
>>rrr=xml.dom.minidom.parseString(open("tt.xml").read())
>>should do.
> or
> rrr = xml.dom.minidom.parse("tt.xml")
thanks a lot guys -- both approaches work a treat.
in particular: diez, thanks for explaining what was going on from
python's perspective
--
http://mail.python.org/mailman/listinfo/python-list
python 2 urlopen vs python 3 urlopen
python 2 :
python
Python 2.7.15 (default, May 15 2018, 15:37:31)
.
>>> import urllib2
>>> res = urllib2.urlopen('https://api.ipify.org').read()
>>> print res
www.xxx.yyy.zzz
python3
python3
Python 3.6.6 (default, Jul 19 2018, 16:29:00)
...
>>> from urllib.request import urlopen
>>> res = urlopen('https://api.ipify.org').read()
>>> print(res)
b'ww.xxx.yyy.zzz'
I'm expecting the python 2 result, just the ip address. How can I get
python 3 just to give the address, and not include b' ' ? I
know I can mess with string manipulation, but I want to keep it
simple.
sean
--
https://mail.python.org/mailman/listinfo/python-list
Re: Running all unit tests
On Feb 6, 9:11 pm, Jason Voegele wrote: > I'm working on my first substantial Python project, and I'm following a fully > test-first approach. I'd like to know how Pythonistas typically go about > running all of their tests to ensure that my application stays "green". > > In Ruby, I would have a Rake task so that I could say "rake test" and all > tests would be executed. In C or C++ I would have a make target so I could > run all my tests with "make test". In Java it would be an Ant task and "ant > test". And so forth and so on. > > What's the recommended approach for Python programs? I'm sure I could write > a shell script (or a Python script even) that scans my "test" directory for > test cases and runs them, but I'm wondering if there's something already > built in that could do this for me. > > -- > Jason Voegele > Only fools are quoted. > -- Anonymous I don't know about the recommended approach, but I've done something like you suggest in a library I authored. Any files named test*.py are found and added to the unittest test suite. See http://code.google.com/p/pydicom/source/browse/trunk/source/dicom/test/run_tests.py. HTH Darcy -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Imaging Library (PIL) question
On Oct 20, 2:14 pm, Sid <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I am tryin to copy an image into my own data structure(a sort of 2d array
> for further FFT). I've banged my head over the code for a couple of hours
> now. The simplified version of my problem is below.
>
> #-Code
>
> import Image
> pic = Image.open("Images/a.jpg")
> (picdata,width,height) = (pic.load(),pic.size[0],pic.size[1])
>
> picMap = [[0] * width ] * height #matrix needed for FFT
>
> print "--Printing PIC MAP--"
> for h in range(0,height):
> for w in range(0,width):
> print picMap[h][w]," ", #everything initialized to ZERO...this is
> ok
> print "\n"
>
> print "--Copying to PIC MAP--"
> for h in range(0, height):
> for w in range(0, width):
> picMap[h][w] = picdata[w,h] #problem lies here
> print picMap[h][w]," ", #Seems to copy perfectly here as the
> output shows
> print "\n"
>
> print "--Printing PIC MAP AGAIN--"
> for h in range(0,height):
> for w in range(0,width):
> print picMap[h][w]," ", #Should print the values as above, but
> doesn't
> print "\n"
>
> #-Code End
>
> Hopefully someone would take a look and let me know what i'm doing
> something wrong here.
>
> Thanks a lot
> -Sid
The problem is likely to do with the line
picMap = [[0] * width ] * height
The first [0]*width creates a list, then the *height repeats the
reference to the same list. See, for example
http://mail.python.org/pipermail/tutor/2001-December/010414.html. It
prints okay in the middle section because the items have just been
stored. Later iterations in that same loop are replacing array
elements that have already been printed. The link explains the problem
and shows a way to avoid this. You might also consider using Numpy, it
has explicit functions to zero any dimension array.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Dummy explanation to win32com needed
On Oct 22, 3:43 pm, korean_dave <[EMAIL PROTECTED]> wrote:
> Hi. I need a dummy's explanation to utilizing the win32com component
> to access Microsoft Excel.
>
> So far, I have this code.
>
> import win32com.client
> xl = win32com.client.Dispatch("Excel.Application")
> xl.Visible = 1
>
> workbook = xl.Workbooks.Open("C:\test.xls")
>
> Now, my question is, where do I find the snytax I can use to access
> such functions like workbook.ActiveSheet.Cells(1,1).Value etc...
>
> I need a reference to the API I can utilize.
>
> Where can I find this?
>
> Thanks guys,
> -Dave
I haven't used this in quite some time, but if I recall correctly, it
simply follows the object model as found in Excel Help | Visual Basic
Reference, or go into the Excel code editor (alt-F11)and use the
object browser. One thing to be aware of, IIRC, is that in VB you can
leave out some default names (like .Value, .Item for collections) but
these must be explicitly referenced when called using win32com.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Calling Python functions from Excel
On Nov 15, 2:20 am, Cannonbiker wrote: > Please I need Calling Python functions from Excel and receive result > back in Excel. Can me somebody advise simplest solution please? I am > more VBA programmer than Python. A couple of years ago I used MSScriptControl for this. Couldn't find a great reference just now, but here is a discussion which should give enough information: http://www.velocityreviews.com/forums/t319222-re-python-in-excel.html Check from around message 3 on. -- http://mail.python.org/mailman/listinfo/python-list
