如何在Python中从文件中读取数字?

如何在Python中从文件中读取数字?,第1张

如何在Python中从文件中读取数字?

假设您没有多余的空格

with open('file') as f:    w, h = [int(x) for x in next(f).split()] # read first line    array = []    for line in f: # read rest of lines        array.append([int(x) for x in line.split()])

您可以将最后一个for循环压缩为嵌套列表推导

with open('file') as f:    w, h = [int(x) for x in next(f).split()]    array = [[int(x) for x in line.split()] for line in f]


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5648161.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存