小编Joh*_*ith的帖子

Python matplotlib多个条形图

如何在matplotlib中绘制多个条形图,当我试图多次调用条形函数时,它们重叠,如下图所示,红色的最高值只能看到.如何在x轴上绘制带有日期的多个条形图?

到目前为止,我试过这个:

import matplotlib.pyplot as plt
import datetime

x = [
    datetime.datetime(2011, 1, 4, 0, 0),
    datetime.datetime(2011, 1, 5, 0, 0),
    datetime.datetime(2011, 1, 6, 0, 0)
]
y = [4, 9, 2]
z = [1, 2, 3]
k = [11, 12, 13]

ax = plt.subplot(111)
ax.bar(x, y, width=0.5, color='b', align='center')
ax.bar(x, z, width=0.5, color='g', align='center')
ax.bar(x, k, width=0.5, color='r', align='center')
ax.xaxis_date()

plt.show()
Run Code Online (Sandbox Code Playgroud)

我懂了:

在此输入图像描述

结果应该是这样的,但是日期在x轴上,并且柱子彼此相邻:

在此输入图像描述

python matplotlib

59
推荐指数
6
解决办法
19万
查看次数

Android中的最大文件名长度

我正在尝试为我正在创建的文件命名.我只是想知道Android中的最大文件名长度是多少?

是否有文件名规范?我可以用这样的字符->

android

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

django全局变量

在views.py中

当我尝试从其他人访问全局变量时def:

def start(request):
    global num
    num=5
    return HttpResponse("num= %d" %num) # returns 5 no problem....

def other(request):
    num=num+1
    return HttpResponse("num= %d" %num)
Run Code Online (Sandbox Code Playgroud)

def other 不回6,但它应该是6对吗?如何在视图中全局访问变量?

python django django-views

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

django剩余的元素被截断

我是django的新手,我想获得名称字段包含"John"的id.下面的代码片段运行得非常好,但是,

在view.py中

 all_ids=Employee.objects.filter(name__contains = 'John').values('id')
    return HttpResponse(" All id= %d " %all_ids)
Run Code Online (Sandbox Code Playgroud)

它返回:

  All id=[{'id': 1},{'id':2} , so on  '...(remaining elements truncated)...'] 
Run Code Online (Sandbox Code Playgroud)

显示项目存在限制.那么,我怎样才能摆脱这种局限并(其余元素被截断)?有没有更好的方法在查询中获取字段的所有值而不截断?

django list

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

Python以分钟为单位减去两个日期

date1 = datetime.datetime(2012, 10, 10, 10, 15, 44)
date2 = datetime.datetime(2012, 10, 17, 8, 45, 38)
roundedA = date2.replace(second = 0, microsecond = 0)
print roundedA
roundedB = date1.replace(second = 0, microsecond = 0)
print roundedB
minutes = (roundedA - roundedB).min
print minutes
Run Code Online (Sandbox Code Playgroud)

结果是:

-999999999 days, 0:00:00
Run Code Online (Sandbox Code Playgroud)

我想计算2个不同的日期差异.我在上面减去了,但它没有给我我想要的东西.如何减去两个日期并在几分钟或几小时内得到结果.

python

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

django 循环遍历模型字段

可能的重复:
Django 获取模型的字段

我有下一个模型:

class People(models.Model):
    name = models.CharField(max_length=100)
    lastname = models.CharField(max_length=100)
Run Code Online (Sandbox Code Playgroud)

我想遍历 People 表中的所有项目。

在views.py中

- 当我尝试这个时:

  for each in People().objects.all():
     name=each.name
Run Code Online (Sandbox Code Playgroud)

我收到那个错误:

Manager isn't accessible via People instances
Run Code Online (Sandbox Code Playgroud)

- 当我尝试这个时:

  for each in People():
     name=each.name
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

'People' object is not iterable
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题以及如何遍历 People 表中的所有项目?

python django

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

Django'int'对象没有属性'status_code'

我从def start方法开始,它调用go_adder在adder.html中添加5次num值,直到num等于5.之后,adder方法应该返回ready = 1

在views.py中

def start(request):
    num=0
    ready_or_not=go_adder(num)
    return HttpResponse("Ready: %s "%str(ready_or_not))

def go_adder(num):
    ready=0
    if num<5:
        return render_to_response('adder.html',{'num':num})
    elif num==5:
        ready=1
        return ready

def check_post(request,num):
    if request.method == 'POST':
        num+=1
        return adder(num)
Run Code Online (Sandbox Code Playgroud)

当我尝试运行此代码段时,它一直有效,直到我的"num = 5",然后我得到了这个错误:

'int' object has no attribute 'status_code' 
Exception Location: C:\Python27\lib\site-packages\django\middleware\common.py in process_response, line 94
Run Code Online (Sandbox Code Playgroud)

和Traceback说:

C:\Python27\lib\site-packages\django\core\handlers\base.py in get_response
                response = middleware_method(request, response) ...
? Local vars
C:\Python27\lib\site-packages\django\middleware\common.py in process_response
        if response.status_code == 404: ...
? Local vars 
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个错误?请你帮助我好吗 ?

django

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

永远不会调用Android BroadcastReceiver

我想实现一个处理屏幕开/关的服务.为此,我创建BootReceiver了在启动时调用并在我启动服务的过程中调用.但是,我ScreenBroadcastReceiver从未被称为,为什么?我是否在错误的地方注册了,我在服务的创建上做了...请帮助我在哪里注册?

因此,应用程序应该在后台运行(没有用户交互),它将记录屏幕的开启和关闭活动.我已经启动了这些代码,但似乎我需要注册我的ScreenBroadcastReceiver广播接收器.如果我在服务的创建上做.我该改变吗?我的最终目标是在后台运行此应用程序,以便应用程序无需用户交互即可运行

这是在一开始就调用的BootReceiver:

public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.w("TAG", "BootReceiver");

        Intent service = new Intent(context, ScreenListenerService.class);
            context.startService(service);

    }

}
Run Code Online (Sandbox Code Playgroud)

这是服务:

public class ScreenListenerService extends Service {


public void onCreate(){
    super.onCreate();
    Log.w("TAG", "ScreenListenerService---OnCreate ");

            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    BroadcastReceiver screenOnReceiver = new ScreenBroadcastReceiver();
    registerReceiver(screenOnReceiver, filter);
}


 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {

        Log.w("TAG", "ScreenListenerService---onStart ");

         return START_STICKY;
    }

@Override
public IBinder onBind(Intent …
Run Code Online (Sandbox Code Playgroud)

android

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

Python脚本在crontab下不起作用

我有一个非常基本的python脚本,每分钟运行一次crontab.

剧本

filed = open('test.txt','a')
Run Code Online (Sandbox Code Playgroud)

crontab的

* * * * * /to path the file/job.py
Run Code Online (Sandbox Code Playgroud)

它应该工作,但我无法看到结果.那么,可能是什么问题呢?

python linux ubuntu cron

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

Django遍历所有要保存的项目,但它只保存一条记录

我有下一个型号:

class People(models.Model):
    name = models.CharField(max_length=100)
    lastname = models.CharField(max_length=100)

class Salary(models.Model):
    id_of_people=models.ForeignKey(People)
    salary = models.IntegerField(required=False)
Run Code Online (Sandbox Code Playgroud)

在views.py中

- 当我尝试这个时:

    for each in People.objects.all()[:3]:
            Salary().id_of_people_id=each.id
            Salary().save()
Run Code Online (Sandbox Code Playgroud)

它只保存3 Th id,而不是1到3.因此,它只节省一次.但是,我想循环遍历所有3条记录,为什么会这样?如何将所有People表ID添加到Salary表?

python django

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

如何从字符串中提取子字符串?

假设我有一个字符串,/Apath1/Bpath2/Cpath3/0-1-2-3-4-5-something.otherthing我只想提取'0-1-2-3-4-5'部分.我试过这个:

str='/Apath1/Bpath2/Cpath3/0-1-2-3-4-5-something.otherhing'

print str[str.find("-")-1:str.find("-")]
Run Code Online (Sandbox Code Playgroud)

但是,结果只有0.如何提取'0-1-2-3-4-5'部分?

python

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

python将字符串转换为字符集

c=['a,b,c','a,b,c','a,b']
for item in c:
    a=set(item.replace("'",""))
    b=list(a)
print b
Run Code Online (Sandbox Code Playgroud)

它返回:

['a','b',',']
Run Code Online (Sandbox Code Playgroud)

我想要的是:

['a','b','c']
Run Code Online (Sandbox Code Playgroud)

如何将这些字符串转换为字符集?

python

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

服务中的onCreate()从未被调用过

我想实现一个处理屏幕开/关的服务.为此,我创建BootReceiver了在启动时调用并在我启动服务的过程中调用.

但是,OnCreate从来没有被称为,为什么?

当我打印程序onCreate的日志时,即使看到日志,我也看不到onStartCommand日志.这怎么可能?请帮我理解这个.

这是BootReceiver在一开始就调用的:

public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.w("TAG", "BootReceiver");

        Intent service = new Intent(context, ScreenListenerService.class);
            context.startService(service);

    }

}
Run Code Online (Sandbox Code Playgroud)

这是服务:

public class ScreenListenerService extends Service {


    public void OnCreate(){
        super.onCreate();
        Log.w("TAG", "ScreenListenerService---OnCreate ");
    }


     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {

            Log.w("TAG", "ScreenListenerService---onStart ");


        }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return …
Run Code Online (Sandbox Code Playgroud)

android android-service

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