Re: What's wrong with my python? I can't use string

2009-02-09 Thread Frank Potter
On Feb 10, 3:20 pm, Chris Rebert  wrote:
> On Mon, Feb 9, 2009 at 11:13 PM, Frank Potter  wrote:
> > I have a xxx.py which has code as below:
>
> > import string
>
> > print dir(string)
> > print string.printable
>
> > when I run it, I got the strange error:
>
> > "\n"
> > "\n"
> > "##result##\n"
> > "##msg##\n"
> > "##pass_no##\n"
> > "##pot_num##\n"
> > "##pots_mashed##\n"
> > "##yb_used##\n"
> > "##right##\n"
> > "\n"
> > ['__builtins__', '__doc__', '__file__', '__name__', 'str']
> > Traceback (most recent call last):
> >  File "del2.py", line 4, in ?
> >    print string.printable
> > AttributeError: 'module' object has no attribute 'printable'
>
> > It seems that I did not import the right string module I want. Can
> > someone give me a clue, please?
>
> Sounds like the wrong `string` module is being imported. What's the
> output of `print string.__file__`?
>
> Cheers,
> Chris
>
> --
> Follow the path of the Iguana...http://rebertia.com

Thank you so much ~
I got it.
I have a string.py in the same directory:(
and when I output string.__file__ as you told me, I know it.

--
http://mail.python.org/mailman/listinfo/python-list


What's wrong with my python? I can't use string

2009-02-09 Thread Frank Potter
I have a xxx.py which has code as below:

import string

print dir(string)
print string.printable


when I run it, I got the strange error:

"\n"
"\n"
"##result##\n"
"##msg##\n"
"##pass_no##\n"
"##pot_num##\n"
"##pots_mashed##\n"
"##yb_used##\n"
"##right##\n"
"\n"
['__builtins__', '__doc__', '__file__', '__name__', 'str']
Traceback (most recent call last):
  File "del2.py", line 4, in ?
print string.printable
AttributeError: 'module' object has no attribute 'printable'

It seems that I did not import the right string module I want. Can
someone give me a clue, please?

--
http://mail.python.org/mailman/listinfo/python-list


how to transfer integer on socket?

2007-04-20 Thread Frank Potter
Is there any easy way to transfer 4 bit integer on socket?

I want to send like this:

a=5
send_integer(socket_s,a)

and receive like this:
a=receive_integer(socket_s)

Sending and receiving is in binary form, not transfer it to string.
Is there any easy way to do this?

-- 
http://mail.python.org/mailman/listinfo/python-list


failed to install PIL in fedora core 6

2007-02-23 Thread Frank Potter
I use "python setup.py install" to install PIL in fedora with python
2.4,
But I got these errors:
running build_ext
building '_imaging' extension
creating build/temp.linux-i686-2.4
creating build/temp.linux-i686-2.4/libImaging
gcc -pthread -fno-strict-aliasing -DNDEBUG -O2 -g -pipe -Wall -Wp,-
D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-
size=4 -m32 -march=i386 -mtune=generic -fasynchronous-unwind-tables -
D_GNU_SOURCE -fPIC -fPIC -DHAVE_LIBZ -I/usr/include/freetype2 -
IlibImaging -I/usr/include -I/usr/local/include -I/usr/include/
python2.4 -c _imaging.c -o build/temp.linux-i686-2.4/_imaging.o
_imaging.c:76:20: error: Python.h: No such file or directory
In file included from libImaging/Imaging.h:14,
 from _imaging.c:78:
libImaging/ImPlatform.h:10:20: error: Python.h: No such file or
directory
libImaging/ImPlatform.h:14:2: error: #error Sorry, this library
requires support for ANSI prototypes.
libImaging/ImPlatform.h:17:2: error: #error Sorry, this library
requires ANSI header files.
libImaging/ImPlatform.h:55:2: error: #error Cannot find required 32-
bit integer type
In file included from _imaging.c:78:
libImaging/Imaging.h:90: error: expected specifier-qualifier-list
before 'INT32'
libImaging/Imaging.h:265: error: expected specifier-qualifier-list
before 'INT32'
libImaging/Imaging.h:393: error: expected '=', ',', ';', 'asm' or
'__attribute__' before 'ImagingCRC32'
_imaging.c:123: error: expected specifier-qualifier-list before
'PyObject_HEAD'
.
_imaging.c: At top level:
_imaging.c:316: error: expected ')' before '*' token
_imaging.c:413: error: expected '=', ',', ';', 'asm' or
'__attribute__' before '*' token
_imaging.c:468: error: expected ')' before '*' token
_imaging.c:536: error: expected '=', ',', ';', 'asm' or
'__attribute__'
.
 before '*' token
_imaging.c:773: error: expected '=', ',', ';', 'asm' or
'__attribute__'  before 'functions'
_imaging.c:3138: warning: return type defaults to 'int'
_imaging.c: In function 'DL_EXPORT':
_imaging.c:3138: error: expected declaration specifiers before
'init_imaging'
_imaging.c:3149: error: expected '{' at end of input
error: command 'gcc' failed with exit status 1

What should I do if I want to successfully have pil installed?

-- 
http://mail.python.org/mailman/listinfo/python-list


Any python scripts to do parallel downloading?

2007-01-31 Thread Frank Potter
I want to find a multithreaded downloading lib in python,
can someone recommend one for me, please?
Thanks~

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: extracting from web pages but got disordered words sometimes

2007-01-27 Thread Frank Potter
Thank you, I tried again and I figured it out.
That's something with beautiful soup, I worked with it a year ago also 
dealing with Chinese html pages and nothing error happened. I read the 
old code and I find the difference. Change the page to unicode before 
feeding to beautiful soup, then everything will be OK.

On Jan 28, 3:26 am, "Paul McGuire" <[EMAIL PROTECTED]> wrote:
> After looking at the pyparsing results, I think I see the problem with
> your original code.  You are selecting only the characters after the
> rightmost "-" character, but you really want to select everything to
> the right of "- -".  In some of the titles, the encoded Chinese
> includes a "-" character, so you are chopping off everything before
> that.
>
> Try changing your code to:
> title=full_title.split("- -")[1]
>
> I think then your original program will work.
>
> -- Paul

-- 
http://mail.python.org/mailman/listinfo/python-list


extracting from web pages but got disordered words sometimes

2007-01-27 Thread Frank Potter
There are ten web pages I want to deal with.
from http://www.af.shejis.com/new_lw/html/125926.shtml
to  http://www.af.shejis.com/new_lw/html/125936.shtml

Each of them uses the charset of Chinese "gb2312", and firefox 
displays all of them in the right form, that's readable Chinese.

My job is, I get every page and extract the html title of it and 
dispaly the title on linux shell Termial.

And, my problem is, to some page, I get human readable title(that's in 
Chinese), but to other pages, I got disordered word. Since each page 
has the same charset, I don't know why I can't get every title in the 
same way.

Here's my python code, get_title.py :

[CODE]
#!/usr/bin/python
import urllib2
from BeautifulSoup import BeautifulSoup

min_page=125926
max_page=125936

def make_page_url(page_index):
return ur"".join([ur"http://www.af.shejis.com/new_lw/
html/",str(page_index),ur".shtml"])

def get_page_title(page_index):
url=make_page_url(page_index)
print "now getting: ", url
user_agent='Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers={'User-Agent':user_agent}
req=urllib2.Request(url,None,headers)
response=urllib2.urlopen(req)
#print response.info()
page=response.read()

#extract tile by beautiful soup
soup=BeautifulSoup(page)
full_title=str(soup.html.head.title.string)

#title is in the format of "title --title"
#use this code to delete the "--" and the duplicate title
title=full_title[full_title.rfind('-')+1::]

return title

for i in xrange(min_page,max_page):
print get_page_title(i)
[/CODE]

Will somebody please help me out? Thanks in advance.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to remove c++ comments from a cpp file?

2007-01-26 Thread Frank Potter
Thank you!

On Jan 26, 6:34 pm, Gabriel Genellina <[EMAIL PROTECTED]> wrote:
> At Friday 26/1/2007 06:54, Frank Potter wrote:
>
> >[CODE]
> >import re
>
> >f=open("show_btchina.user.js","r").read()
> >f=unicode(f,"utf8")
>
> >r=re.compile(ur"//[^\r\n]+$", re.UNICODE|re.VERBOSE)
> >f_new=r.sub(ur"",f)
>
> >open("modified.js","w").write(f_new.encode("utf8"))
> >[/CODE]
>
> >And, the problem is, it seems that only the last comment is removed.
> >How can I remove all of the comments, please?Note that it's not as easy as 
> >simply deleting from // to end of line,
> because those characters might be inside a string literal. But if you
> can afford the risk, this is a simple way without re:
>
> f = open("show_btchina.user.js","r")
> modf = open("modified.js","w")
> for line in f:
>  uline=unicode(line,"utf8")
>  idx = uline.find("//")
>  if idx==0:
>  continue
>  elif idx>0:
>  uline = uline[:idx]+'\n'
>  modf.write(uline.encode("utf8"))
> modf.close()
> f.close()
>
> --
> Gabriel Genellina
> Softlab SRL
>
> __
> Preguntá. Respondé. Descubrí.
> Todo lo que querías saber, y lo que ni imaginabas,
> está en Yahoo! Respuestas (Beta).
> ¡Probalo ya!http://www.yahoo.com.ar/respuestas

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to remove c++ comments from a cpp file?

2007-01-26 Thread Frank Potter

On Jan 26, 5:08 pm, Gary Herron <[EMAIL PROTECTED]> wrote:
> Frank Potter wrote:
> > I only want to remove the comments which begin with "//".
> > I did like this, but it doesn't work.
>
> > r=re.compile(ur"//[^\r\n]+$", re.UNICODE|re.VERBOSE)
> > f=file.open("mycpp.cpp","r")
> > f=unicode(f,"utf8")
> > r.sub(ur"",f)
>
> > Will somebody show me the right way?
> > Thanks~~If you expect help with a problem, it would be nice if you told us 
> > what
> the problem is. What error did you get?
>
> But even without that I see lots of errors:
>
> You must import re before you use it:
> import re
>
> Open a file with open((..) not file.open(...).
>
> Once you open the file you must *read* the contents and operate on that:
> data = f.read()
>
> Then you ought to close the file:
> f.close()
>
> Now you can do your sub on the string in data -- but note, THIS WON'T
> CHANGE data, but rather returns a new string which you must assign to
> something:
>
> new_data = r.sub(ur"", data)
>
> Then do something with the new string.
>
> Also I fear your regular expression is incorrect.
>
> Cheers,
> Gary Herron

Thank you.
I'm very sorry because I was in a hurry when I post this thread.
I'll post again my code here:
[CODE]
import re

f=open("show_btchina.user.js","r").read()
f=unicode(f,"utf8")

r=re.compile(ur"//[^\r\n]+$", re.UNICODE|re.VERBOSE)
f_new=r.sub(ur"",f)

open("modified.js","w").write(f_new.encode("utf8"))
[/CODE]

And, the problem is, it seems that only the last comment is removed.
How can I remove all of the comments, please?

-- 
http://mail.python.org/mailman/listinfo/python-list


how to remove c++ comments from a cpp file?

2007-01-26 Thread Frank Potter
I only want to remove the comments which begin with "//".
I did like this, but it doesn't work.

r=re.compile(ur"//[^\r\n]+$", re.UNICODE|re.VERBOSE)
f=file.open("mycpp.cpp","r")
f=unicode(f,"utf8")
r.sub(ur"",f)

Will somebody show me the right way?
Thanks~~

-- 
http://mail.python.org/mailman/listinfo/python-list


Is any python like linux shell?

2007-01-21 Thread Frank Potter
I learned some python in windows.
And now I've turned to linux.
I read a book and it teaches how to write shell script with bash,
but I don't feel like the grammar of bash.
Since I know about python,
I want to get a linux shell which use python grammar.
I searched by google and I found pysh, which is not maintained any
more.
There's another script named pyshell, which is not likely what I'm
searching for.
So, will somebody please tell me if there are any python like shells
for linux?

-- 
http://mail.python.org/mailman/listinfo/python-list


how to write unicode to a txt file?

2007-01-17 Thread Frank Potter
I want to change an srt file to unicode format so mpalyer can display
Chinese subtitles properly.
I did it like this:

txt=open('dmd-guardian-cd1.srt').read()
txt=unicode(txt,'gb18030')
open('dmd-guardian-cd1.srt','w').write(txt)

But it seems that python can't directly write unicode to a file,
I got and error at the 3rd line:
UnicodeEncodeError: 'ascii' codec can't encode characters in position
85-96: ordinal not in range(128)

How to save the unicode string to the file, please?
Thanks!

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: where is python on linux?

2007-01-07 Thread Frank Potter
Thank you!
"which python" works for me.
I got it.
Peter Otten wrote:
> Frank Potter wrote:
>
> > I installed fedora core 6 and it has python installed.
> > But the question is, where is the executable python file?
> 
> Find out yourself with
> 
> $ which python
> 
> Peter

-- 
http://mail.python.org/mailman/listinfo/python-list


where is python on linux?

2007-01-07 Thread Frank Potter
I installed fedora core 6 and it has python installed.
But the question is, where is the executable python file?
I can't find it so I come here for help.
I want to config pydev for eclipse and I need to know where the
ececutable python file is.
Thank you!

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what are you using python language for?

2006-06-07 Thread Frank Potter
I use python to grab imformations and embed it in my delphi app. I
gather what I am intereted in so I can read them faster.

And I also use python to do some othere everyday jobs, such as
periodically copying and zipping a directory into another computer to
backup the comtent.

On 7 Jun 2006 16:11:54 -0700, Petr Jakes <[EMAIL PROTECTED]> wrote:
> hacker1017 wrote:
> > im just asking out of curiosity.
> a vending machine controlled from the PC (peripherals connected using
> I2C bus (SMBus) and the MDB coin change-giver and the bill acceptor
> connected to the  serial port).
>
> Petr Jakes
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Regular Expression question

2006-06-07 Thread Frank Potter
pyparsing is cool.
but use only re is also OK
# -*- coding: UTF-8 -*-
import urllib2
html=urllib2.urlopen(ur"http://www.yahoo.com/";).read()

import re
r=re.compile('[^"]+)"[^>]*>',re.IGNORECASE)
for m in r.finditer(html):
print m.group('image')

I got these rusults:
http://us.i1.yimg.com/us.yimg.com/i/ww/beta/edit_plink.gif
http://us.i1.yimg.com/us.yimg.com/i/ww/bt1/125.gif
http://us.i1.yimg.com/us.yimg.com/i/ww/bt1/13441.gif
http://us.i1.yimg.com/us.yimg.com/i/ww/bt1/136.gif
http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif
http://us.i1.yimg.com/us.yimg.com/i/ww/bt1/ml.gif
http://us.i1.yimg.com/us.yimg.com/i/ww/bt1/my.gif
http://us.i1.yimg.com/us.yimg.com/i/ww/bt1/msgn.gif
http://us.i1.yimg.com/us.yimg.com/i/ww/v5_mail_t2.gif
http://us.i1.yimg.com/us.yimg.com/i/ww/news/2006/06/07/0607notorious_big.jpg
http://us.i1.yimg.com/us.yimg.com/i/ww/beta/wthr.gif
http://us.i1.yimg.com/us.yimg.com/i/mntl/sh/04q2/camera.gif

On 6/8/06, Paul McGuire <[EMAIL PROTECTED]> wrote:
> <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> > Hi,
> > I am new to python regular expression, I would like to use it to get an
> > attribute of an html element from an html file?
> >
> > for example, I was able to read the html file using this:
> >req = urllib2.Request(url=acaURL)
> > f = urllib2.urlopen(req)
> >
> > data = f.read()
> >
> > my question is how can I just get the src attribute value of an img
> > tag?
> > something like this:
> > (.*)(.*)
> >
> > I need to get the href of the image source.
> >
> > Thanks.
> >
>
> As Fredrik pointed out, re's are not the only tool out there.  Here's a
> pyparsing solution.
>
> -- Paul
>
>
> import pyparsing
> import urllib
>
> # define HTML tag format using makeHTMLTags helper
> # (we don't really care about the ending  tag,
> # even though makeHTMLTags returns definitions for both
> # starting and ending tag patterns)
> imgStartTag, dummy = pyparsing.makeHTMLTags("img")
>
> # get HTML source from some web site
> htmlPage = urllib.urlopen("http://www.yahoo.com";)
> htmlSource = htmlPage.read()
> htmlPage.close()
>
> # scan HTML source, printing SRC attribute from each  tag
> for tokens,start,end in imgStartTag.scanString(htmlSource):
> print tokens.src
>
>
> Prints:
>
> http://us.i1.yimg.com/us.yimg.com/i/ww/beta/edit_plink.gif
> http://us.i1.yimg.com/us.yimg.com/i/ww/bt1/125.gif
> http://us.i1.yimg.com/us.yimg.com/i/ww/bt1/13441.gif
> http://us.i1.yimg.com/us.yimg.com/i/ww/bt1/136.gif
> http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif
> http://us.i1.yimg.com/us.yimg.com/i/ww/bt1/ml.gif
> http://us.i1.yimg.com/us.yimg.com/i/ww/bt1/my.gif
> http://us.i1.yimg.com/us.yimg.com/i/ww/bt1/msgn.gif
> http://us.i1.yimg.com/us.yimg.com/i/ww/v5_mail_t2.gif
> http://us.i1.yimg.com/us.yimg.com/i/mntl/aut/06q2/hea_0411.gif
> http://us.i1.yimg.com/us.yimg.com/i/mntl/aut/06q2/img_0607.jpg
> http://us.i1.yimg.com/us.yimg.com/i/ww/news/2006/06/07/0607notorious_big.jpg
> http://us.i1.yimg.com/us.yimg.com/i/ww/beta/news/video.gif
> http://us.i1.yimg.com/us.yimg.com/i/buzz/2006/06/wholefoodssmall.jpg
> http://us.i1.yimg.com/us.yimg.com/i/mntl/msg/06q2/img_im.jpg
> http://us.i1.yimg.com/us.yimg.com/i/ww/trfc_bckt.gif
> http://us.i1.yimg.com/us.yimg.com/i/mntl/sh/04q2/camera.gif
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


how to login a newsgroup programmely and fetch its emails to read?

2006-04-05 Thread Frank Potter
how to login a newsgroup and get its informations by an account and a password?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Delete a file

2006-02-17 Thread Frank Potter
system.remove(filename)

On 2/18/06, Sbaush <[EMAIL PROTECTED]> wrote:
> Hi.
> In my application i create a PNG image and i view it in a frame.
> How can delete it from my python code?
> Bye...
>
> --
> Sbaush
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


How to *Search* with google from inside my programme and get the search result?

2006-02-14 Thread Frank Potter
I want to search something by a key word from inside my py script.
The using google idea comes to my mind first because write a search
programme from scratch is not so easy.

I want to take advantage of goolge results, but I don't know how.
To extract the result from html of google can get the result, but it's not
reliable and stable because the html structure my be changed any time.

Does google supply some webservice to programmers? I did see
some a tool use google to search and use it's result from the programme very
well. Its name is "Email catcher&sender 2.10", which can be download
at http://www.worldminer.com/download.htm. Windows users can take a
look at it. It supports more than google, but yahoo and some other search
engines. How does it do it? I really need some hints.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: removing characters before writing to file

2006-02-08 Thread Frank Potter
r=re.compile(r"(\(')|( '\))|'")print r.sub('',str(output))On 8 Feb 2006 18:35:01 -0800, [EMAIL PROTECTED]
 <[EMAIL PROTECTED]> wrote:
hii have some output that returns a lines of tuples egtr('sometext1', 1421248118, 1, 'P ')('sometext2', 1421248338, 2, 'S ')and so onI tried thisre.sub(r" '() ",'',str(output)) but it only get rid of the ' and not
the braces. I need to write the output to a file such thatsometext1, 1421248118, 1, Psometext2, 1421248338, 2, SI also tried escaping , re.sub(r" '\(\) ",'',str(output)) but also didnot work
How can i get rid of the braces before writing to file? thanks--http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Is Python string immutable?

2005-12-07 Thread Frank Potter
Thank you very much.Steve Holden, I post my soucecode at my blog here:http://hiparrot.wordpress.com/2005/12/08/implementing-a-simple-net-spider/
I wish you can read and give me some suggestion. Any comments will be appreciated.On 12/2/05, Steve Holden <
[EMAIL PROTECTED]> wrote:could ildg wrote:> In java and C# String is immutable, str=str+"some more" will return a
> new string and leave some gargabe.> so in java and C# if there are some frequent string operation,> StringBuilder/StringBuffer is recommanded.>> Will string operation in python also leave some garbage? I implemented a
> net-spider in python which includes many html string procession. After> it running for sometime, the python exe eats up over 300M memory. Is> this because the string garbages?>If you create garbage in a Python program it will normally be collected
and returned to free memory by the garbage collector, which should berun when memory is exhausted in preference to allocating more memory.Additional memory should therefore only be claimed when garbagecollection fails to return sufficient free space.
If cyclic data structures are created (structures in which componentsrefer to each other even though no external references exist) this couldcause problems in older versions of Python, but nowadays the garbage
collector also takes pains to collect unreferenced cyclic structures.> If String in python is immutable, what class should I use to avoid too> much garbages when processing strings frequently?>
The fact that your process uses 300MB implies that you are retainingreferences to a large amount of data. Without seeing the code, however,it's difficult to suggest how you might improve the situation. Are you,
for example, holding the HTML for every spidered page?As a side note, both C# and Java also use garbage collection, so if youralgorithm exhibits the same problem in all three languages this merelyconfirms that the problem really is your algorithm, and not the language
in which it is implemented.regards  Steve--Steve Holden   +44 150 684 7255  +1 800 494 3119Holden Web LLC www.holdenweb.comPyCon TX 2006  
www.python.org/pycon/--http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list