您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python-从文本文件中读取数字并放入列表

Python-从文本文件中读取数字并放入列表

with open('data.txt') as f:
    polyShape = []
    for line in f:
        line = line.split() # to deal with blank 
        if line:            # lines (ie skip them)
            line = [int(i) for i in line]
            polyShape.append(line)

会给你

[[0, 0, 3, 50], [50, 100, 4, 20]]

这将适用于包含(或不包含)空行的文件

with完成操作或遇到异常时,使用该构造将自动为您关闭文件

python 2022/1/1 18:25:20 有351人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶