Reading a file into a multidimensional array with Python

前端 未结 5 1296
迷失自我
迷失自我 2021-02-20 02:56

If I have a text file like this:

Hello World
How are you?
Bye World

How would I read it into a multidimensional array like this:



        
5条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-20 03:09

    Also don't forget to use strip to remove the \n:

    myArray = []
    textFile = open("textFile.txt")
    lines = textFile.readlines()
    for line in lines:
        myArray.append(line.split(" "))
    

提交回复
热议问题