首页 > 解决方案 > 如何读取 .array 文件并将数字传输到 Python 中的空列表中

问题描述

我正在尝试从名为sample.arrayPython 的空列表中的文件中提取数字。

我已经尝试过通常的方式来读取文件,但结果却一无所获。

下面是我的代码和图片sample.array

import sys
import numpy as np

def read_file(filename):
    """
    Your file will need to extract numbers from an array and 
    """
    list_of_numbers = []
    file = open(sys.argv[1]).readline()
    for number in file:
        list_of_numbers.append(number)

print(list_of_numbers)

样本数组

标签: pythonarrays

解决方案


暗示:

  1. 将文件作为单个字符串读取(谷歌它!)
  2. 然后拆分字符串。(谷歌这个呢!)。

推荐阅读