import json
array = '{"fruits": ["apple", "banana", "orange"]}'
data = json.loads(array)
Run Code Online (Sandbox Code Playgroud)
这是我的JSON数组,但我想将fruits字符串中的所有值转换为Python列表.这样做的正确方法是什么?
my_string = """This is my first line,
this is my second line, and...
...this is my fourth line!"""
Run Code Online (Sandbox Code Playgroud)
如何将that(This is my first line,)的第一行存储到单独的字符串中?我尝试使用.readline()另一个类似的问题,但是我得到了这个错误:
AttributeError: 'str' object has no attribute 'readline'
我有一个大图像.我希望在它自己的网页上显示它,并且在没有任何CSS的情况下执行此操作时,它会填满整个页面并且太大而无法立即显示(滚动条出现).我想缩小它以使其适合用户屏幕的高度(而不是宽度).我已经找到了解决方案,但这些解决方案可以拉伸图像以适应整个屏幕 - 我不希望这样,因为它会破坏纵横比.
基本上,我想在保持纵横比的同时调整图像大小.我该怎么做呢?
这是一个示例字符串:
123456#p654321
Run Code Online (Sandbox Code Playgroud)
目前,我正在使用此匹配捕获123456并654321进入两个不同的组:
([0-9].*)#p([0-9].*)
Run Code Online (Sandbox Code Playgroud)
但有时,#p654321字符串的一部分将不存在,所以我只想捕获第一组.我试图通过追加?它来使第二组"可选" ,这是有效的,但只要#p剩下的字符串末尾有一个.
解决这个问题的最佳方法是什么?
我正在使用该subprocess模块并check_output()在我的Python脚本中创建一个虚拟shell,它适用于返回零退出状态的命令,但对于那些不返回的命令,它会返回异常,而不会打印本来会显示的错误普通shell上的输出.
例如,我希望有些东西能像这样工作:
>>> shell('cat non-existing-file')
cat: non-existing-file: No such file or directory
Run Code Online (Sandbox Code Playgroud)
但相反,这发生了:
>>> shell('cat non-existing-file')
CalledProcessError: Command 'cat non-existing-file' returned non-zero exit status 1 (file "/usr/lib/python2.7/subprocess.py", line 544, in check_output)
Run Code Online (Sandbox Code Playgroud)
即使我可以使用try和删除Python异常消息except,我仍然希望cat: non-existing-file: No such file or directory显示给用户.
我该怎么做呢?
shell():
def shell(command):
output = subprocess.check_output(command, shell=True)
finished = output.split('\n')
for line in finished:
print line
return
Run Code Online (Sandbox Code Playgroud) 这是我的示例脚本:
import ConfigParser
config = ConfigParser.ConfigParser()
config.read('conf.ini')
print bool(config.get('main', 'some_boolean'))
print bool(config.get('main', 'some_other_boolean'))
Run Code Online (Sandbox Code Playgroud)
这是conf.ini:
[main]
some_boolean: yes
some_other_boolean: no
Run Code Online (Sandbox Code Playgroud)
运行脚本时,它会打印True两次.为什么?它应该是False,如some_other_boolean设置的那样no.
是否有任何实用程序可用于将图像转换为ASCII然后在我的终端中打印?我找了一个但似乎找不到任何东西.
默认情况下,(对我来说)每个urlopen()带参数的似乎都会发送一个POST请求.如何设置调用以发送GET?
import urllib
import urllib2
params = urllib.urlencode(dict({'hello': 'there'}))
urllib2.urlopen('http://httpbin.org/get', params)
Run Code Online (Sandbox Code Playgroud)
urllib2.HTTPError:HTTP错误405:不允许方法
工人:
def worker():
while True:
fruit, colour = q.get()
print 'A ' + fruit + ' is ' + colour
q.task_done()
Run Code Online (Sandbox Code Playgroud)
将项目放入队列:
fruit = 'banana'
colour = 'yellow'
q.put(fruit, colour)
Run Code Online (Sandbox Code Playgroud)
输出:
>>> A banana is yellow
Run Code Online (Sandbox Code Playgroud)
我怎么能做到这一点?我试了一下ValueError: too many values to unpack,然后我意识到我q.put()将两个变量放入队列.
有没有办法将一组"变量/对象"放入一个队列项中,就像我试图做的那样?
我想使用Python创建一个简单的"虚拟bash"脚本,它接受命令并返回stdio输出.像这样的东西:
>>> output = bash('ls')
>>> print output
file1 file2 file3
>>> print bash('cat file4')
cat: file4: No such file or directory
Run Code Online (Sandbox Code Playgroud)
有没有人知道允许这种情况发生的模块/功能?我找不到一个.