相关疑难解决方法(0)

无法理解为什么字母u前缀我的raw_input()输出

我目前正在学习Python,并编写了一个程序来试验这门语言.但是,每当我使用它时,输出总是在其中有一个字母"u".我正在使用Pyscripter作为我的IDE.

这是我的代码:

print "whats your name"
age = raw_input()
print "Alright, so %r, I just realized what percent-r does actually or is meant for" % (age)
print "What next ur age",
age1 = raw_input()
print "you entered %r " % (age1)
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我看到这样的事情:

>>> Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32.

>>> whats your name (i typed kk)

>>> Alright, so u'kk', i just realized what percent-r does actually or is meant for

>>> …
Run Code Online (Sandbox Code Playgroud)

python unicode raw-input

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

遇到格式化程序的问题:"%支持的操作数类型:'NoneType'和'tuple'"

我收到这个错误,我不明白为什么.

我正在学习使用Gmail的API并复制粘贴他们的示例:https://developers.google.com/gmail/api/v1/reference/users/threads/get#examples

这是代码:

def GetThread(service, user_id, thread_id):
  """Get a Thread.

  Args:
    service: Authorized Gmail API service instance.
    user_id: User's email address. The special value "me"
    can be used to indicate the authenticated user.
    thread_id: The ID of the Thread required.

  Returns:
    Thread with matching ID.
  """
  try:
    thread = service.users().threads().get(userId=user_id, id=thread_id).execute()
    messages = thread['messages']
    print ('thread id: %s - number of messages '
           'in this thread: %d') % (thread['id'], len(messages))
    return thread
  except errors.HttpError, error:
    print 'An …
Run Code Online (Sandbox Code Playgroud)

python

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

如何在Python中打印行

我在Python中遇到过这样的行:

print "Let's talk about %s." % my_name
Run Code Online (Sandbox Code Playgroud)

但是,我在Ruby中看到了这两个具有相同输出的情况:

print "Let's talk about #{my_name}."

print "Let's talk about %s." % my_name"

是否只有一种方法可以在Python中打印相同的行?提前致谢!

ruby python

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

字符串格式:更好地使用'%'或'格式'?

我使用python 3.4,我可以用两种方式格式化字符串:

print("%d %d" %(1, 2))
Run Code Online (Sandbox Code Playgroud)

print("{:d} {:d}".format(1, 2))
Run Code Online (Sandbox Code Playgroud)

文档中,他们仅使用"格式"显示示例.这是否意味着使用'%'并不好,或者使用哪个版本并不重要?

python string

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

简单查询上的 Python MySQL 语法错误

我正在为 python 3.x 使用 MySQL 连接器,并且使用简单的代码遇到了这个问题,例如:

rest_cursor.execute("SELECT * FROM restaurant WHERE r_id=%s",(r_id))

其中 r_id 是一个整数。这导致抛出异常:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s' at line 1

这种格式似乎与 python-MySQL 示例页面中的示例相匹配:http : //dev.mysql.com/doc/connector-python/en/connector-python-example-cursor-select.html

我究竟做错了什么?谢谢

python mysql

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

Python2中的Python3 f字符串替代

大多数情况下,我使用Python3。我可以写这个吗

print(f"the answer is {21 + 21} !")
Run Code Online (Sandbox Code Playgroud)

输出:答案是42!但是在Python 2中f字符串不存在。那么这是最好的方法吗?

print("the answer is " + str(21 + 21) + "!")
Run Code Online (Sandbox Code Playgroud)

python python-2.7 python-3.x f-string

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

字符串中的 django/python 变量?

我是 python 新手,需要一些帮助。

我有一个变量

q = request.GET['q']
Run Code Online (Sandbox Code Playgroud)

如何在其中插入变量q

url = "http://search.com/search?term="+q+"&location=sf"
Run Code Online (Sandbox Code Playgroud)

现在我不确定公约是什么?我习惯了 PHP 或 javascript,但我正在学习 python,如何动态插入变量?

python django variables dynamic

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

在 Python 中返回格式化的字符串

我有一个字符串:

testString = """ My name is %s
and I am %s years old
and I live in %s"""
Run Code Online (Sandbox Code Playgroud)

我有代码可以找到我想输入到 testString 中的这三个字符串。我怎么做?在 Java 中,我会这样做:

return String.format(testString, string1, string2, string3)
Run Code Online (Sandbox Code Playgroud)

Python中是否有等价物?

python string format

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