小编Rob*_*rto的帖子

在Python请求中获取状态代码的描述

我希望能够输入服务器响应代码并让请求告诉我代码的含义.例如,代码200 - >确定

我找到了源代码的链接,它显示了代码和描述的字典结构.我看到Requests将返回给定描述的响应代码:

print requests.codes.processing  # returns 102
print requests.codes.ok          # returns 200
print requests.codes.not_found   # returns 404
Run Code Online (Sandbox Code Playgroud)

但不是相反:

print requests.codes[200]        # returns None
print requests.codes.viewkeys()  # returns dict_keys([])
print requests.codes.keys()      # returns []
Run Code Online (Sandbox Code Playgroud)

我认为这将是一项例行任务,但似乎无法在在线搜索或文档中找到答案.

python http python-requests

27
推荐指数
3
解决办法
2万
查看次数

matplotlib 如何填充_between 阶跃函数

我试图遮蔽输入信号高(值 = 1)的绘图区域。该区域应保持阴影直到信号变低(值 = 0)。我已经非常接近了,以下是一些示例:http : //matplotlib.org/examples/pylab_examples/axhspan_demo.html在 matplotlib 图中,我可以突出显示特定的 x 值范围吗? 如何在 Python 中使用 Matplotlib 绘制阶跃函数?

问题是现在它只在信号 = 1 的地方直接着色,而不是到信号 = 0的下一个变化(阶跃函数)。例如,在下面的图像/代码中,我希望将绘图填充在 20-40 和 50-60 之间(而不是 20-30,以及低于 40 的峰值)。如何修改我的代码以实现这一目标?谢谢。 输出图显示不正确的阴影

import numpy as np
import matplotlib.pyplot as plt

x = np.array([0,10,20,30,40,50,60])
s = np.array([0,0,1,1,0,1,0])
t = np.array([25,24,25,25,24,25,24])

fig, ax = plt.subplots()

ax.plot(x,t)
ax.step(x,s,where='post')

# xmin xmax ymin ymax
plt.axis([0,60,0,30])

ymin, ymax = plt.ylim()
# want this to fill until the next "step"
# i.e. should be filled …
Run Code Online (Sandbox Code Playgroud)

python matplotlib

5
推荐指数
1
解决办法
5200
查看次数

为什么`print`在Python多处理pool.map中不起作用

我正在尝试实现该multiprocessing模块以使用大型csv文件.我正在使用Python 2.7并按照此处的示例进行操作.

我运行了未经修改的代码(为方便起见,下面复制了代码)并注意到函数中的print语句worker不起作用.无法print理解流程和调试.

任何人都可以解释为什么print不在这里工作?pool.map不执行打印命令吗?我在网上搜索但没有找到任何表明这一点的文件.

import multiprocessing as mp
import itertools
import time
import csv

def worker(chunk):
    # `chunk` will be a list of CSV rows all with the same name column
    # replace this with your real computation
    print(chunk)     # <----- nothing prints
    print 'working'  # <----- nothing prints
    return len(chunk)  

def keyfunc(row):
    # `row` is one row of the CSV file.
    # replace this with the name …
Run Code Online (Sandbox Code Playgroud)

python multiprocessing

5
推荐指数
1
解决办法
7930
查看次数

在openpyxl中使用`append`时如何应用单元格样式?

openpyxl用来创建Excel工作表.我想在插入数据时应用样式.麻烦的是该append方法获取数据列表并自动将它们插入到单元格中.我似乎无法指定要应用于此操作的字体.

事后我可以返回并将样式应用于单个单元格,但这需要开销才能找出列表中有多少数据点,以及我当前追加到哪一行.有没有更简单的方法?

这个说明性代码显示了我想要做的事情:

def create_xlsx(self, header):
    self.ft_base = Font(name='Calibri', size=10)
    self.ft_bold = self.ft_base.copy(bold=True)
    if header:
        self.ws.append(header, font=ft_bold) # cannot apply style during append
Run Code Online (Sandbox Code Playgroud)

python-2.7 openpyxl

5
推荐指数
1
解决办法
3526
查看次数

如何解压缩列表(字典!)并作为分组元组返回?

我有一个由混合字典和列表组成的数据结构.我试图解压缩这个以获得键的元组和每个键的所有子值.

我正在使用列表推导,但只是没有让它工作.我哪里错了?

我看到了有关拆包列表中,列出了许多其他的答案(如1,2),但找不到一个例子,其中对多个子值的单个键解包.

  • 期望的输出 - > [('A',1,2),('B',3,4)]
  • 实际输出 - > [('A',1),('A',2),('B',3),('B',4)]

码:

dict_of_lists = {'A':[{'x':1},{'x':2}], 'B':[{'x':3},{'x':4}] }
print [(key,subdict[subkey],) for key in dict_of_lists.keys() for subdict in dict_of_lists[key] for subkey in subdict.keys()]
Run Code Online (Sandbox Code Playgroud)

python dictionary iterable-unpacking

5
推荐指数
1
解决办法
5982
查看次数

numpy vstack投掷维度错误

我正在监视串口并尝试在Matplotlib到达时绘制数据.因为数据是以不规则的间隔到达的,所以我使用的方法来附加数据 - 类似于这个线程.

这是我的代码:

data = np.zeros(shape=(1,1), dtype=[('millis',float),('temperature_Celsius',float),('relative_humidity',float),('setpoint',float),('relay_status',float)])
print data # gives a 1-row, 5-element tuple: [[(0.0, 0.0, 0.0, 0.0, 0.0)]]

# append the new row
# throws error regarding array dimensions
data = np.vstack(( data, [(1,2,3,4,5)] ))
Run Code Online (Sandbox Code Playgroud)

我无法正确获得尺寸标注,因为我收到以下错误:

ValueError: all the input array dimensions except for the concatenation axis must match exactly
Run Code Online (Sandbox Code Playgroud)

请帮助识别语法错误.

在Python 2.6,Numpy 1.8,Windows 7上运行.

python numpy

1
推荐指数
1
解决办法
5070
查看次数

由于缺少"Application"引用而导致Outlook代码编译错误

我正在尝试编写一些c#代码与Outlook 2010进行交互.我目前正在使用Microsoft的这个示例.

我的代码如下:

using System;
using System.Text;          // StringBuilder
using System.Diagnostics;   // Debug

using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;

using Outlook = Microsoft.Office.Interop.Outlook;

namespace DirectReports
{
    public class Program
    {
        private void GetManagerDirectReports()
        {
            Outlook.AddressEntry currentUser = Application.Session.CurrentUser.AddressEntry;
            //Outlook.AddressEntry currentUser = Outlook.Application.Session.CurrentUser.AddressEntry;
            if (currentUser.Type == "EX")
            {
                Outlook.ExchangeUser manager = currentUser.GetExchangeUser().GetExchangeUserManager();
                if (manager != null)
                {
                    Outlook.AddressEntries addrEntries =
                        manager.GetDirectReports();
                    if (addrEntries != null)
                    {
                        foreach (Outlook.AddressEntry addrEntry
                            in addrEntries)
                        {
                            Outlook.ExchangeUser exchUser = addrEntry.GetExchangeUser();
                            StringBuilder sb = …
Run Code Online (Sandbox Code Playgroud)

c# outlook

1
推荐指数
1
解决办法
2470
查看次数

Python 测试字符串是否与模板值匹配

我试图遍历一个字符串列表,只保留那些与我指定的命名模板匹配的字符串。我想接受任何与模板完全匹配的列表条目,而不是在变量<SCENARIO>字段中包含一个整数。

检查需要是一般的。具体来说,字符串结构可能会发生变化,以至于无法保证<SCENARIO>总是出现在字符 X 处(例如,使用列表推导式)。

下面的代码显示了一种使用 的方法split,但必须有更好的方法来进行此字符串比较。我可以在这里使用正则表达式吗?

template = 'name_is_here_<SCENARIO>_20131204.txt'

testList = ['name_is_here_100_20131204.txt',        # should accept
            'name_is_here_100_20131204.txt.NEW',    # should reject
            'other_name.txt']                       # should reject

acceptList = []

for name in testList:
    print name
    acceptFlag = True
    splitTemplate = template.split('_')
    splitName = name.split('_')
    # if lengths do not match, name cannot possibly match template
    if len(splitTemplate) == len(splitName):
        print zip(splitTemplate, splitName)
        # compare records in the split
        for t, n in zip(splitTemplate, splitName):
            if t!=n and …
Run Code Online (Sandbox Code Playgroud)

python regex string

0
推荐指数
1
解决办法
3518
查看次数