易截截图软件、单文件、免安装、纯绿色、仅160KB

【求助】关于python中字节数组转换成字符串

由于是初学,在些请教大家。

Python code:

from urllib import request
import struct
class csdn:
def __init__(self):
print ('Hello,this is a init')
def getContent(self,url):
self.url = url
result = request.urlopen(self.url).read()#这里返回一个byte数组
#print(type(result))
saveFile = open('f:\\csdn\\1.txt','w')
try:
saveFile.write(struct.pack("s",result))
finally:
saveFile.close()

test = csdn()
test.getContent('http://www.baidu.com')


查资料说用PACK可以,但我写了上面的那个TEST,执行不出来,或者还有什么别的方法,可以从byte[] to String 的,告诉我一下,谢谢。
我的目的是想把返回的结果存到一个文件里。

直接 saveFile.write(result) 不就可以了么?

出错,如下错误:
Python code:

Traceback (most recent call last):
File "F:\Information\Python\test\networkTest.py", line 17, in <module>
test.getContent('http://www.baidu.com')
File "F:\Information\Python\test\networkTest.py", line 12, in getContent
saveFile.write(result)
TypeError: write() argument 1 must be str, not bytes



因为你的程序里有汉字,要在代码第一行加:

# -*-


相关问答:

python正则表达式 如何取出以下字符串 多谢

s='aaa111aaa,bbb222,333ccc,444ddd444,555eee666,fff777ggg'
用正则表达式取出 前后字母相同的数据 结果如下:
111 ddd
谢谢~
Python code:

import re
s='aaa111aaa,bbb222,333ccc,444ddd444,555eee666,ff ......

python怎么print 'hello world'

大家好,我是一个新手,刚开始学python,但是刚开始的helloworld都没法打印,让我很无奈。
我的python安装路径为f:\python31。在path中也设置对了,在windows下运行是这样显示的,希望各位前辈指点一下。谢谢
Pyth ......

python 正则匹配中文

在文本文件中匹配项包含中文
如内容为:gamename=中文 
  key=天下
文本文件的编码的文件为utf-8
python代码如下:
# -*- coding:UTF-8 -*-
  contents=open(from_pa ......

python 3.x的if语句

>>> x=-1
>>> if x>0:
print ('11')

>>> else:

SyntaxError: invalid syntax (<pyshell#20>, line 1)
网上搜的也是:
>>> x=int(input("Please ent ......

开始学习python, 大家多多指点

刚刚买了一本《Python核心编程》,开始学习python。大家多多指教

电子书就很多。。。

《Python核心编程》这书的第一版还是不错的,第二版很不咋的,真的。

个人的意见是:初学用《Dive in Python》, ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号