小编abc*_*ccd的帖子

python 2to3中的eval(input())

从Python 2to3 doc:

input:

转换input(prompt)eval(input(prompt))

我正在尝试使用Python 2几年后学习Python 3.任何人都可以解释为什么工具eval在调用之前插入input,以及我是否应该在所有Python 3代码中执行此操作?

python python-2to3

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

有没有办法用pip install运行2to3?

我正在尝试使用pip install -r requirements.txt.来维护依赖项.但是,某些必需的包不直接支持Python 3,但可以使用手动转换2to3.

在做什么时有办法强制自动pip运行2to3这些包pip install -r requirements.txt吗?

python pip python-2to3 requirements.txt

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

Python & Pandas:如何进行条件计算

df['direction']是风向的编号,范围为 1-16。我想把它转换成360-degree system. #1方向是90#267.5,它们顺时针运行。

我可以df['degree'] = 90-(df.direction-1)*22.5,但这会产生negative value,你可以看到下面的输出 ![在此处输入图片说明] 1

但我不知道如何使用conditionalhere, for df['degree']<0, then + 360。我怎样才能做到这一点?

python pandas

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

python中的“全局名称未定义”概念

我正在学习 Python 并阅读了有关此错误的博客,但我仍然无法清楚地理解这一点。这是我正在编写的代码片段:

for i in included:
    global signs,accounts, regions
    global sign_name, acc_name, rg_name
    if type == "regions":
        regions = i
        rg_name = regions['data']['region']
    if type == "accounts":
        accounts = i
        acc_name = accounts['data']['account']

    print("Stopping account " + acc_name + " in region " + rg_name)
Run Code Online (Sandbox Code Playgroud)

NameError: 全局名称 'acc_name' 未定义。

我正在使用 Python 2.7 如果有人能帮助我理解 Python 中全局名称和起始的概念,那就太好了。提前致谢。

python global-variables

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

使用一个变量定义多个函数 docstring

我有几个函数(a,bc),我希望它们使用相同的文档字符串。所以我的计划是通过只写一次文档字符串并将其保存到变量来保存行DOCSTRING。然后我把它放在函数声明下。我在PEP 257中没有找到任何可以解决我的问题的内容......

DOCSTRING = '''
This is a docstring
for functions:
a,
b,
c'''

def a(x, y):
    DOCSTRING
    # do stuff with x and y

def b(x, y):
    DOCSTRING
    # do other stuffs with x and y

def c(x, y):
    DOCSTRING
    # do some more stuffs with x and y

help(a), help(b), help(c)
Run Code Online (Sandbox Code Playgroud)

我实际上认为它可能有效......但我错了,我得到了这个:

Help on function a in module __main__:

a(x, y)

Help on function b in module __main__: …
Run Code Online (Sandbox Code Playgroud)

python docstring

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

当机器人加入服务器时发送消息

每当机器人被邀请到服务器时,我都想发送一条消息。然后它应该写一些类似的东西:“你好,这是我的不和谐机器人”

到目前为止,我有这段代码,它不会产生错误,但也不会发送消息。

@bot.event
async def on_server_join(ctx):
    for guild in bot.guilds:
        for channel in guild.text_channels:
            if channel.permissions_for(guild.me).say:
                await ctx.message.channel.send('Hello! \n')
                break
Run Code Online (Sandbox Code Playgroud)

python discord discord.py discord.py-rewrite

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

UnicodeDecodeError:“ charmap”编解码器无法解码位置X的字节0x9d:字符映射到&lt;undefined&gt;

当我尝试使用pip 安装StringGenerator时,系统出现以下错误提示:

C:\Users\Administrator> pip install StringGenerator

Collecting StringGenerator 
Using cached StringGenerator-0.3.0.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\ADMINI~1\AppData\Local\Temp\2\pip-build-mdvrj2cf\StringGenerator\setup.py", line 7, in <module>
    long_description = file.read()
  File "c:\users\administrator\appdata\local\programs\python\python36-32\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 1264: character maps to <undefined>

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in C:\Users\ADMINI~1\AppData\Local\Temp\2\pip-build-mdvrj2cf\StringGenerator\
Run Code Online (Sandbox Code Playgroud)

python windows pip

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

为什么多个on_message事件不起作用?

为什么不能有多个on_message活动?

import discord

client = discord.Client()

@client.event
async def on_ready():
    print('in on_ready')

@client.event
async def on_message(message):
    print("in on_message #1")

@client.event
async def on_message(message):
    print("in on_message #2")

@client.event
async def on_message(message):
    print("in on_message #3")

client.run("TOKEN")
Run Code Online (Sandbox Code Playgroud)

例如,如果我输入不一致的内容,则总是只有最后on_message一个被触发。我怎样才能使这三个都正常工作?

python discord discord.py

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

检查数字是否为回文而不将其更改为字符串

我在解决这个问题时遇到了麻烦,如果一个数字n是回文,则只返回 True 或 False 。

注:哪里有a____表示哪里有需要填写的空格,有2个空格。

def is_palindrome(n):
    x, y = n, 0
    f = lambda: ____
    while x > 0:
        x, y = ____ , f()
    return y == n
Run Code Online (Sandbox Code Playgroud)

我在这上面花了大约一个小时。我发现放入x//10第二个空格将允许函数迭代n. 然后归结为函数f

理想情况下,每次调用它时,它都应该将最后一位数字添加n到一个新数字中y。因此,如果n = 235,while 循环将迭代 3 次,并且每次f()被调用,它应该将5, 3, 和2,添加到 value y

python iteration palindrome

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

导入错误:无法导入名称“isclose”

在对我的 Python 模块进行单元测试时,我遇到了一个奇怪的错误:

\n\n

\xe2\x85\x94 的构建正常通过,但其中之一无法isclose从标准math库导入。

\n\n

该错误重现如下:

\n\n
==================================== ERRORS ====================================\n______________________ ERROR collecting tests/test_yau.py ______________________\nImportError while importing test module \'/home/travis/build/Benjamin-Lee/squiggle/tests/test_yau.py\'.\nHint: make sure your test modules/packages have valid Python names.\nTraceback:\ntests/test_yau.py:5: in <module>\n    from math import isclose\nE   ImportError: cannot import name \'isclose\'\n!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!\n=========================== 1 error in 0.29 seconds ============================\nThe command "pytest --cov=squiggle" exited with 2.\n
Run Code Online (Sandbox Code Playgroud)\n\n

在同一目录中没有指定的文件math.py(或者在我的包中根本没有)。可能是什么原因造成的?

\n\n

多次重新启动构建并没有修复这个错误,它只出现在 Python 3.4 中。

\n\n

完整日志可在此处访问。

\n

python math pytest travis-ci python-3.4

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