免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2268 | 回复: 9
打印 上一主题 下一主题

Help: How to fix it ? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-09 00:29 |只看该作者 |倒序浏览
Below code snippet got choked in pyhton3.0 while it does job in python2.5
I highlighted the line that caused the breaking in red color and the compiler says

Traceback (most recent call last):
  File "ph.py", line 312, in <module>
     key = string.strip( key )
AttributeError: 'module' object has no attribute 'strip'

I'm quite new to Python.  please tell me what is wrong and how to fix it.
Thanks in advance.

Regards,

if __name__ == '__main__':
    if len(sys.argv) == 1:
        sys.stdout = sys.stderr
        print ('Usage: %s <input filename>' % sys.argv[0])
        print ('  The input file contains pairs of lines.  The first line')
        print (' each pair is a string containing a key.  The second line')
        print (' is an integer which is the desired hash code.')
        sys.exit()

    input = open(sys.argv[1], 'r')
    keys = []
    while 1:
        key = input.readline()
        if key == "": break
        hashcode = input.readline()
        if hashcode == "":
            sys.stderr.write('Odd number of lines in file\n')
            sys.exit()

        key = string.strip( key )
        hashcode = int(hashcode)
        keys.append( (key,hashcode) )

    input.close()
    sys.stderr.write('%i key/hash pairs read\n' % len(keys) )
    generate_hash( keys )

论坛徽章:
0
2 [报告]
发表于 2009-05-09 07:59 |只看该作者
改成
key = key.strip(" ")
试试

如果key结尾包含回车符的话:
key = key.rstrip("\n")
key = key.strip(" ")

[ 本帖最后由 千年沉寂 于 2009-5-9 08:25 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2009-05-09 08:55 |只看该作者
Hi 千年沉寂,
    your solutions are cool ! I still have another function has the same issue:
    def __init__(self, N):
        self.N = N
        junk = ""
        for i in range(10):
            junk = junk + random.choice(string.letters + string.digits)
        self.junk = junk

The compiler says
    junk = junk + random.choice(string.letters + string.digits)
AttributeError: 'module' object has no attribute 'letters'

How to fix it ? Thank you so much !!!

论坛徽章:
0
4 [报告]
发表于 2009-05-09 09:54 |只看该作者
import string
......

junk = junk + random.choice(string.ascii_letters + string.digits)

#####
Public string module variables:
ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
digits = '0123456789'
hexdigits = '0123456789abcdefABCDEF'
octdigits = '01234567'
printable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTU...
punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
whitespace = ' \t\n\r\x0b\x0c'


>>> # You will get the doc about 'string' module if you try like follow
>
>> import string
>>> help(string)
Help on module string:
..........


[ 本帖最后由 千年沉寂 于 2009-5-9 10:02 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2009-05-09 10:35 |只看该作者
Oh yeah !  Thank you very much ! by the way, where can I find out the string module's document ?
is there something like perldoc in perl ? thanks a lot.

论坛徽章:
0
6 [报告]
发表于 2009-05-09 10:45 |只看该作者
Oh yeah !  Thank you very much ! by the way, where can I find out the string module's document ?
is there something like perldoc in perl ? thanks a lot.

论坛徽章:
0
7 [报告]
发表于 2009-05-09 11:04 |只看该作者

回复 #5 mjus 的帖子

Have not learned Perl, so I do not know anything about PerlDoc.
If u intend to learn more about string module, maybe u can read the code of string.py.
The path of string.py is like this:C:\python31\lib\string.py

论坛徽章:
0
8 [报告]
发表于 2009-05-09 11:08 |只看该作者

论坛徽章:
0
9 [报告]
发表于 2009-05-09 11:39 |只看该作者
your informative feedback is invaluable. many thanks !

论坛徽章:
0
10 [报告]
发表于 2009-05-09 12:56 |只看该作者
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP