- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 Python 新手。我正在尝试创建一个程序来打印我通常每周手动打印的一组文档,但是我遇到了几个问题:
这是代码:
import os
file_list = os.listdir("C:/Python27/Programs/PrintNgo/Files2print")
print ("List of available documents to print" '\n')
enum_list = ('\n'.join('{}: {}'.format(*k) for k in enumerate(file_list)))
print(enum_list)
user_choice = input('\n' "Documents # you want to print: ")
copies = input("How many copies would you like from each: ")
#not implemented
current_choice = file_list[user_choice]
current_file = os.startfile("C:/Python27/Programs/PrintNgo/Files2print/"+current_choice, "print")
这是输出:
List of available documents to print
0: doc0.docx
1: doc1.docx
2: doc2.docx
3: doc3.docx
4: doc4.docx
5: doc5.docx
Documents # you want to print:
我设法输入 0-5 之间的数字并打印所需的文档,但是输入 2 个值,例如:2,3 不起作用并引发错误。如何一次打印多张?
如果我想复制每个文档。假设我选择了 2,3,我是否应该循环重复每个操作,次数与我想要的副本数相同?
最佳答案
您应该避免使用 Python 2 中的 input
函数。它很方便,但存在安全风险。相反,您应该使用 raw_input
函数。在 Python 3 中,名为 input
的函数相当于 Python 2 的 raw_input
,并且旧 Python 2 input
函数的功能已被删除。
下面的代码显示了如何处理以逗号分隔的列表中给出的多个文档编号。该代码还处理单个文档编号。如果用户提供任何非整数值,程序将崩溃并出现 ValueError
。但是,输入中允许有空格。
from __future__ import print_function
user_choice = raw_input("\nDocuments # you want to print: ")
user_choice = [int(u) for u in user_choice.split(',')]
copies = int(raw_input("How many copies would you like from each: "))
for i in range(copies):
print('Copy', i + 1)
for j in user_choice:
print('Printing document #', j)
演示
Documents # you want to print: 3, 2,7
How many copies would you like from each: 2
Copy 1
Printing document # 3
Printing document # 2
Printing document # 7
Copy 2
Printing document # 3
Printing document # 2
Printing document # 7
这段代码的核心是str.split
方法。
user_choice.split(',')
获取user_choice
中的字符串并将其拆分为字符串列表,在找到逗号的地方进行拆分,并丢弃逗号。
[int(u) for u in user_choice.split(',')]
获取每个结果字符串并将它们转换为整数,将结果存储在列表中。
关于Python:根据用户输入打印一个或多个文件(副本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39677132/
不确定我的标题措辞是否正确,但请耐心等待,所有内容都会得到解释... 我们有一组代码不是在这里发明的,它使用进程间通信(IPC 消息传递)。该方案的大致轮廓是这样的: comms.c 包含: stat
你怎么能列出所有的颠覆合并? 例如: Trunk ____9_____14____20___ \ \ \ \______\_____\___
是否有一个集合的标准 Java(1.5+)实现(即无第三方),允许我将多个集合粘合到一个集合中? 这是其工作原理的草图: final SomeCollection x = new SomeCollec
有没有办法让sql查询返回拆分行。我什至不知道怎么问。下面有'index_tag'。 select event.name, tb_ev.start_time, tb_ev.end_time from
我正在尝试使用 postgresql COPY 命令从 CSV 加载一些数据。诀窍是我想在用户标识(包含在 CSV 中)上实现 Multi-Tenancy 。加载 csv 时,是否有一种简单的方法告诉
我正在尝试使用 bash 脚本将文件复制到当前目录。 为了处理需要转义的路径,使用了一个变量,该变量被转义然后提供给 cp 命令。 cp 命令提示: usage: cp [-R [-H | -L |
我正在尝试每 20 毫秒向给定的 x 和 y 坐标添加一次 CAShapelayer。我希望形状在一秒钟内消失(就像示踪剂一样)。我创建的功能有效,形状在正确的位置创建并消失。但是我留下了额外的形状,
我是 Python 新手。我正在尝试创建一个程序来打印我通常每周手动打印的一组文档,但是我遇到了几个问题: 这是代码: import os file_list = os.listdir("C:/Pyt
我有一个大小为 10 的 ArrayList l1。我将 l1 分配给新的列表引用类型 l2。 l1 和 l2 会指向同一个 ArrayList 对象吗?或者是 ArrayList 对象的副本分配给
我这周花了一个自由职业者创建的 Mongo 4.4 PSA 副本来工作。我放弃了,从所有三台服务器上删除了完整的 mongod,然后按照 Mongo doc 从头开始安装。 .唯一的变化是在副本初
设置信息: 我有两个数据中心,每个 DC 中有 5 个节点。 我知道插入到表中的每一行都是根据使用的数据分区方案存储的;生成必要的副本并将它们存储在集群中的其他节点(根据复制策略选择节点)上。给定一行
我对 XSLT 完全陌生,所以请耐心等待。 我有两个 xml 文件,我试图使用 XSLT 将它们连接在一起。我想合并这些文件,以便第二个文件中指定的任何值覆盖第一个文件。例如 firstFile.xm
这里肯定有一个初学者问题,为什么 F# 编译器会制作不必要的 DateTimeOffset 副本,我该如何阻止它?我不记得这是个问题,但也许自从我在 F# 中使用 DateTimeOffset 以来已
我有一个用 C# 编写的 WinForms 应用程序,在将数据从 SQL 数据库导出到模板的工作表之前,它使用以下代码打开 Excel 模板。 Microsoft.Office.Interop.Exc
我从这个 post 得到的 xsl 中有这个函数 用“换行符”替换“cr” 我是这样调用它的: 我正在做文章链接,点击文本“阅读更多”
所以这可能有点难以解释...... 目前我这样做: SomeInterface xyz1 = SomeInterface.method(data); SomeInterfaceCopy xyz2 =
我有一个包含指针 p 的类型 var。我需要在另一个与 var 类型相同的变量 var1 上复制 var(通过在引号中执行 var1 "="var,因为我不知道这是否是正确的方法,请参见下文)。 在我
出于某种原因,我需要同时运行两个 xampp 副本。我在互联网上阅读了很多教程,但如果我需要运行另一个,他们最终会告诉我关闭当前的 xampp。这有可能实现吗? 最佳答案 您可以使用不同的端口同时运行
在aws中,“upload-part-copy”具有字节范围选项。如果我想将两个对象的一部分复制到云中的新对象,我可以使用“upload-part-copy”命令进行复制。 我找不到任何此类方法或机制
我有一个带栏的表 foo foo --- bar 我使用 Postgres 的 Copy 命令 COPY (select * from foo) TO 'complete_file_path' WIT
我是一名优秀的程序员,十分优秀!