显然,以下是有效的语法
my_string = b'The string'
Run Code Online (Sandbox Code Playgroud)
我想知道:
b字在前面的字符串是什么意思?我在SO上找到了一个相关的问题,但是这个问题是关于PHP的,它表示b用于表示字符串是二进制的,而不是Unicode,这是代码与PHP版本兼容所需的代码<6 ,当迁移到PHP 6.我不认为这适用于Python.
我确实在Python网站上找到了关于使用相同语法的字符将字符串指定为Unicode的文档u.不幸的是,它没有提到该文档中任何地方的b字符.
而且,只是出于好奇,有没有比多符号b和u是做其他事情?
我正在创建一个程序,它将从Web服务器下载.jar(java)文件,方法是读取同一游戏/应用程序的.jad文件中指定的URL.我正在使用Python 3.2.1
我设法从JAD文件中提取JAR文件的URL(每个JAD文件都包含JAR文件的URL),但是您可以想象,提取的值是type()字符串.
这是相关的功能:
def downloadFile(URL=None):
import httplib2
h = httplib2.Http(".cache")
resp, content = h.request(URL, "GET")
return content
downloadFile(URL_from_file)
Run Code Online (Sandbox Code Playgroud)
但是我总是得到一个错误,说上面函数中的类型必须是字节,而不是字符串.我尝试过使用URL.encode('utf-8'),还有字节(URL,encoding ='utf-8'),但我总是得到相同或类似的错误.
所以基本上我的问题是当URL存储在字符串类型中时如何从服务器下载文件?
虽然从移植代码python2到3,我从一个URL读取时出现此错误
TypeError:initial_value必须是str或None,而不是字节.
import urllib
import json
import gzip
from urllib.parse import urlencode
from urllib.request import Request
service_url = 'https://babelfy.io/v1/disambiguate'
text = 'BabelNet is both a multilingual encyclopedic dictionary and a semantic network'
lang = 'EN'
Key = 'KEY'
params = {
'text' : text,
'key' : Key,
'lang' :'EN'
}
url = service_url + '?' + urllib.urlencode(params)
request = Request(url)
request.add_header('Accept-encoding', 'gzip')
response = urllib.request.urlopen(request)
if response.info().get('Content-Encoding') == 'gzip':
buf = StringIO(response.read())
f = gzip.GzipFile(fileobj=buf)
data …Run Code Online (Sandbox Code Playgroud) 如何将zip解压缩到内存中?
我尝试(返回None上.getvalue()):
from zipfile import ZipFile
from StringIO import StringIO
def extract_zip(input_zip):
return StringIO(ZipFile(input_zip).extractall())
Run Code Online (Sandbox Code Playgroud) 我试过了
import urllib.request
Run Code Online (Sandbox Code Playgroud)
要么
import urllib
Run Code Online (Sandbox Code Playgroud)
我的urllib的路径是
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/__init__.py
我想知道urlopen在哪里,或者我的python模块指向错误的文件?
我正在编写一个脚本,它将自动更新已安装的Calibre版本.目前我已经下载了最新的便携版本.我似乎无法保存zipfile.目前我的代码是:
import urllib2
import re
import zipfile
#tell the user what is happening
print("Calibre is Updating")
#download the page
url = urllib2.urlopen ( "http://sourceforge.net/projects/calibre/files" ).read()
#determin current version
result = re.search('title="/[0-9.]*/([a-zA-Z\-]*-[0-9\.]*)', url).groups()[0][:-1]
#download file
download = "http://status.calibre-ebook.com/dist/portable/" + result
urllib2.urlopen( download )
#save
output = open('install.zip', 'w')
output.write(zipfile.ZipFile("install.zip", ""))
output.close()
Run Code Online (Sandbox Code Playgroud) 我正在研究移动应用程序,需要在解压缩.apk文件后分析他们的代码.然而,解压缩的过程自然涉及大量的IO,这使得它不具有可扩展性,我在想是否可以将解压缩的数据保存在内存中,其中有几个变量代表它,从而节省了写入FS的麻烦.我有成千上万的应用程序需要分析,所以能够做这样的事情会大大加快我的过程.是否有人可以为我提出建议.我正在使用python.提前致谢