私信  •  关注

accdias

accdias 最近创建的主题
accdias 最近回复了
5 年前
回复了 accdias 创建的主题 » 通过python中的列表下载PDF文件

问题是您正在定义函数 main() 但你不能在别的地方叫它。

下面是一个完整的实现您所需的示例:

import wget


def main():
    books_folder = 'C:/Users/ALEXJ/OneDrive/Desktop/Books'
    books_list = 'list.txt'

    with open(books_list) as books:
        for book in books:
            wget.download(book.strip(), books_folder)
            print('Downloaded', book)


if __name__ == '__main__':
    main()
5 年前
回复了 accdias 创建的主题 » 在python中逐行分析文本

正如我在评论中所说 rdline = fp.readline() fp 在你的for循环中。

with open(filepath) as f:
    for line in f: 
        do_something(line)
5 年前
回复了 accdias 创建的主题 » 在Python中,如何水平而不是垂直地执行此操作?

为了换一种方式(更像是一种蟒蛇式的方式),下面是我的看法:

from random import randint


def roll_dice(n):
    return [randint(1, 6) for _ in range(n)]


print('You re-rolled some dice and the new values are:',
    ', '.join(map(str, roll_dice(5)))
)

或者,如果你想要更好的可视化 print() 学生:

print('You re-rolled some dice and the new values are: ', end='')
print(*roll_dice(5), sep=', ')

最后,如果您不关心不以逗号分隔的值,您可以简单地:

print('You re-rolled some dice and the new values are:', *roll_dice(5))

这里是这个概念的一个证明:

Python 3.7.5 (default, Oct 17 2019, 12:16:48) 
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from random import randint
>>> 
>>> 
>>> def roll_dice(n):
...     return [randint(1, 6) for _ in range(n)]
... 
>>> 
>>> print('You re-rolled some dice and the new values are:',
...     ', '.join(map(str, roll_dice(5)))
... )
You re-rolled some dice and the new values are: 4, 3, 5, 5, 4
>>>