相关疑难解决方法(0)

如何在Python中的String中放置一个变量?

我想int加入一个string.这就是我现在正在做的事情:

num = 40
plot.savefig('hanning40.pdf') #problem line
Run Code Online (Sandbox Code Playgroud)

我必须运行几个不同数字的程序,而不是两个40.所以我想做一个循环但插入这样的变量不起作用:

plot.savefig('hanning', num, '.pdf')
Run Code Online (Sandbox Code Playgroud)

如何将变量插入Python字符串?

python string variables

218
推荐指数
6
解决办法
64万
查看次数

TypeError:在字符串格式化python期间不是所有参数都被转换

该程序应该采用两个名称,如果它们的长度相同,则应检查它们是否是同一个单词.如果它是相同的单词,它将打印"名称相同".如果它们长度相同但字母不同,则会打印"名称不同但长度相同".我遇到问题的部分是在底部4行.

#!/usr/bin/env python
# Enter your code for "What's In (The Length Of) A Name?" here.
name1 = input("Enter name 1: ")
name2 = input("Enter name 2: ")
len(name1)
len(name2)
if len(name1) == len(name2):
    if name1 == name2:
        print ("The names are the same")
    else:
        print ("The names are different, but are the same length")
    if len(name1) > len(name2):
        print ("'{0}' is longer than '{1}'"% name1, name2)
    elif len(name1) < len(name2):
        print ("'{0}'is longer than '{1}'"% …
Run Code Online (Sandbox Code Playgroud)

python string typeerror output-formatting python-3.x

178
推荐指数
6
解决办法
47万
查看次数

PyLint消息:logging-format-interpolation

对于以下代码:

logger.debug('message: {}'.format('test'))
Run Code Online (Sandbox Code Playgroud)

pylint 产生以下警告:

记录格式插值(W1202):

在日志记录函数中使用%格式并将%参数作为参数传递当日志语句的调用形式为"logging.(format_string.format(format_args ...))"时使用.此类调用应使用%格式,但通过将参数作为参数传递,将插值留给日志记录功能.

我知道我可以关掉这个警告,但我喜欢理解它.我假设使用format()是打印输出语句的首选方法pylint.为什么记录器语句不适用?

python pylint python-3.x

128
推荐指数
4
解决办法
3万
查看次数

如何创建只有一个元素的元组

在下面的例子中,我希望所有元素都是元组,为什么元组只包含一个字符串时转换为字符串?

>>> a = [('a'), ('b'), ('c', 'd')]
>>> a
['a', 'b', ('c', 'd')]
>>> 
>>> for elem in a:
...     print type(elem)
... 
<type 'str'>
<type 'str'>
<type 'tuple'>
Run Code Online (Sandbox Code Playgroud)

python

88
推荐指数
3
解决办法
4万
查看次数

我收到类型错误。我如何解决它?

我经常从我的 Python 代码中得到未捕获的异常(错误),这些异常被描述为TypeErrors. 经过大量的实验和研究,我收集了以下示例(以及细微的变化):

TypeError: func() takes 0 positional arguments but 1 was given
TypeError: func() takes from 1 to 2 positional arguments but 3 were given
TypeError: func() got an unexpected keyword argument 'arg'
TypeError: func() missing 1 required positional argument: 'arg'
TypeError: func() missing 1 required keyword-only argument: 'arg'
TypeError: func() got multiple values for argument 'arg'
TypeError: MyClass() takes no arguments
TypeError: unsupported operand type(s) for +: 'int' and 'str'
TypeError: can only concatenate str …
Run Code Online (Sandbox Code Playgroud)

python debugging typeerror

11
推荐指数
2
解决办法
8049
查看次数

打印命名元组

在Python 2.7.1中,我可以创建一个命名元组:

from collections import namedtuple
Test = namedtuple('Test', ['this', 'that'])
Run Code Online (Sandbox Code Playgroud)

我可以填充它:

my_test = Test(this=1, that=2)
Run Code Online (Sandbox Code Playgroud)

我可以这样打印:

print(my_test)
Run Code Online (Sandbox Code Playgroud)

测试(这= 1,那= 2)

但为什么我不能这样打印呢?

print("my_test = %r" % my_test)
Run Code Online (Sandbox Code Playgroud)

TypeError:并非在字符串格式化期间转换所有参数

编辑: 我应该知道在Python中使用字符串格式化打印元组

python namedtuple

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

python元组打印问题

print '%d:%02d' % divmod(10,20)
Run Code Online (Sandbox Code Playgroud)

得到我想要的:

0:10
Run Code Online (Sandbox Code Playgroud)

然而

print '%s %d:%02d' % ('hi', divmod(10,20))
Run Code Online (Sandbox Code Playgroud)

结果是:

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    print '%s %d:%02d' % ('hi', divmod(10,20))
TypeError: %d format: a number is required, not tuple
Run Code Online (Sandbox Code Playgroud)

如何修复第二个print语句以使其有效?

我认为有一个更简单的解决方案

m = divmod(10,20)
print m[0], m[1]
Run Code Online (Sandbox Code Playgroud)

或使用python 3或format().

我觉得我错过了一些明显的东西

python tuples string-formatting

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