易截截图软件、单文件、免安装、纯绿色、仅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 PIL模块打开图片问题

请问下,我的是python2.5安装了PIL模块,然后编写程序打开本机上的一个图片时,为什么老出现错误呢,老是找不到图片,错误提示如下:File "C:\Python25\Lib\site-packages\PIL\Image.py", line 1888, in o ......

IIS 7 python 2.6 cgi 文件上传问题

我每次上传的文件读到的数据都不正确。2M 的图片读得10多K 。。哪位大侠可以帮帮我啊。
#!D:\ProgrammerTools\python26\python.exe
#encoding=utf-8
import cgitb
import os
cgitb.enable()
import cgi,urllib ......

python urllib urllib2 httplib 访问url的问题

我想用python解析网页中的url 在下载之前, 我想先发一个head请求 不需要下载整个网页, 先获得该网站的head信息, 读取一些信息之后 在调用这些库的read方法来读取整个网页 该怎么操作呢 多谢
帮顶了

有人能指教一 ......

用python写的数据转发程序

#coding:utf-8
import socket,time,sys
def proxy_server():
    client_addr = 0
    conn = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    conn.setsockopt ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号