小编jul*_*ria的帖子

如何设置一个带有一个带有aphanumeric字符串的参数的URL?

我正在做的事情:

url(r'^confirmemail/[a-bA-z0-9]+/', 'blog.views.confirmemail'),
Run Code Online (Sandbox Code Playgroud)

确切的网址是这样的:http://127.0.0.1:8000/confirmemail/tubwp2n0a6http://127.0.0.1:8000/confirmemail/tubwp2n0a6/

字符串参数的长度为十(10).

如何在urls.py中写入该url?

python django url django-urls

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

变量分配给最后执行的行?

我刚刚在Python解释器中发现了一些奇怪的东西.让我演示给你看:

$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> _
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '_' is not defined
>>> 5 + 4
9
>>> _
9
>>> 'Hello world'
'Hello world'
>>> _
'Hello world'
>>> type(3.5)
<type 'float'>
>>> _
<type 'float'>
Run Code Online (Sandbox Code Playgroud)

你可以在翻译中试试这个; 这里没有招数!

最后执行的行的结果是否被赋值给名为_?的变量?

有人知道吗?有没有关于它的文件?在哪种情况下它可能有用吗?

python interpreter

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

无法从文本文件中打印特定行

所以我目前有这个代码来读取一个看起来像这样的accounts.txt文件:

username1:password1
username2:password2
username3:password3
Run Code Online (Sandbox Code Playgroud)

然后我有了这个(感谢这里的成员)阅读accounts.txt文件并将其拆分为用户名和密码,以便我以后可以打印它.当我尝试使用与此代码分开的用户名和密码打印第1行时:

with open('accounts.txt') as f:

    credentials = [x.strip().split(':') for x in f.readlines()]



for username,password in credentials:

    print username[0]
    print password[0]
Run Code Online (Sandbox Code Playgroud)

它打印出来:

j
t
a
2
a
3
Run Code Online (Sandbox Code Playgroud)

(这些是我在文本文件中的三行,正确分割,但它打印所有行,只打印每行的第一个字母.)

我尝试了几种不同的方法,没有运气.任何人都知道该怎么做?

谢谢你的帮助.非常感谢.这是我的第二天编程,我为这么简单的问题道歉.

python mechanize python-3.x mechanize-python

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

哪一个更像蟒蛇?

我正在探索以前没用过的各种语言,使用简单的Perl脚本作为我想要完成的事情的基础.我有几个版本的东西,我很好奇哪个是使用Python时的首选方法 - 或者如果不是,那是什么?

版本1:

workflowname = []
paramname = []
value = []
for line in lines:
        wfn, pn, v = line.split(",")
        workflowname.append(wfn)
        paramname.append(pn)
        value.append(v)
Run Code Online (Sandbox Code Playgroud)

版本2:

workflowname = []
paramname = []
value = []
i = 0;
for line in lines:
        workflowname.append("")
        paramname.append("")
        value.append("")
        workflowname[i], paramname[i], value[i] = line.split(",")
        i = i + 1
Run Code Online (Sandbox Code Playgroud)

就个人而言,我更喜欢第二种,但正如我所说,我很好奇真正了解Python的人会更喜欢什么.

python

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

将字符串转换为列表

假设我有这样的字符串:

string = ("['1.345', '1.346', '1.347']")
Run Code Online (Sandbox Code Playgroud)

所以它的格式就像一个列表,但我需要将它转换为实际的列表对象.我怎样才能做到这一点?

python list

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

如何在没有使用Django ORM的数百个查询的情况下选择多对一到多个?

我的数据库有以下架构:

class Product(models.Model):
    pass

class Tag(models.Model):
    product = models.ForeignKey(Product)
    attr1 = models.CharField()
    attr2 = models.CharField()
    attr3 = models.CharField()

class AlternatePartNumber(models.Model):
    product = models.ForeignKey(Product)
Run Code Online (Sandbox Code Playgroud)

换句话说,a Product有很多Tags,而a Product有很多AlternatePartNumbers. Tags是一组属性的集合Product.

给定a中的三个属性Tag,我想选择Product匹配的s(可能多于一个),以及AlternatePartNumber每个产品的所有s.

目前我这样做:

# views.py
results = Tag.objects.
    filter(attr1=attr1).
    filter(attr2=attr2).
    filter(attr3=attr3)

# a template
{% for result in results %}
    {% for alternate in result.product.alternatepartnumber_set.all %}
        {{ alternate.property }}
    {% endfor %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

这可以运行数千个查询,具体取决于匹配的数量.有没有一种优化方法?我试过使用Tag.objects.select_related().filter...,这有些帮助,但它没有帮助.

python django query-optimization django-models django-orm

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

如果输入是x的倍数,如何采取不同的行动?

我的程序要求用户输入,但我想拒绝任何26的倍数(或输入0)并要求用户再次输入.我无法弄清楚如何做到这一点; 我猜这是将输入除以26得到一个整数.

目前的代码是:

  ValidInput = False  
  while ValidInput == False: 
    try: 
      Key = int(input('Enter the amount that shifts the plaintext alphabet to the ciphertext alphabet: ')) 
    except: 
      print("Sorry, that isn't an integer. ") 
    else: 
      ValidInput = True 
  return Key
Run Code Online (Sandbox Code Playgroud)

python

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

渲染前将表单“作者”字段设置为登录用户

我想将表单“作者”字段 (models.ForeignKey(User)) 的默认值设置为当前登录的用户。我想在保存条目之前执行此操作,因此在创建条目时,用户不必为了表单验证而选择自己。

我尝试在表单 init 函数中设置该值,但是没有对请求对象的引用,所以似乎不可能?

我没有为此应用程序使用 django 管理面板。

django

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

Try-Catch和Delete []问题

我遇到了一个try-catch情况的问题,这里是代码(这很简单):

struct Something
{
  int A, B, C;
  Something()
  {
    A = B = C = 0;
  }
  ~Something()
  {
    cout << "Destructor " << endl;
  }
};

int main()
{
  Something * s = new Something;

  //Something * s = new Something[5];

  try
  {
    delete[] s;
  }
  catch(exception& e)
  {
    delete s;
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我打算做的是首先尝试将指针删除为数组,如果失败,则进行简单删除.

所以,当我第一次尝试使用'Something'(如在注释行中)的数组时,itr工作得很好.但是,当我现在尝试它时,我得到一个可怕的错误.

有任何想法吗?

c++ exception-handling try-catch delete-operator

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

程序在Thread.sleep()期间并与Timer冻结

原始问题:

应该使用此方法将JFrame上显示的图像逐渐更改为另一图像。但是,如果没有某种方法可以减慢它的运行速度,它似乎只能从一个图像更改为新图像。为了减慢速度,我放入Thread.sleep(1000),这样更改不会立即发生。但是,有了这一行,我的程序完全死机了。没有错误信息,什么都没有。有人可以帮我吗?建议一种更好的方法来降低它的速度,或解决该问题的方法。

为了澄清起见:int k是更改中的渐进步骤数。k = 1将是即时变化。更大的事情将是逐渐的变化。同时,int控制每个图像显示多少的比例。

public void morphImg(int width, int height, BufferedImage morphImage, int k) {
    //creates new image from two images of same size
    BufferedImage image2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            //get color from original image
            Color c = new Color(image.getRGB(i, j));

            //get colors from morph image
            Color c2 = new Color(morphImage.getRGB(i, j));

            for (int l = 1; …
Run Code Online (Sandbox Code Playgroud)

java sleep bufferedimage try-catch freeze

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