Reading a file into a multidimensional array with Python

前端 未结 5 1222
迷失自我
迷失自我 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:07

    Adding to the accepted answer:

    with open("textFile.txt") as textFile:
        lines = [line.strip().split() for line in textFile]
    

    This will remove '\n' if it is appended to the end of each line.

提交回复
热议问题