Python File to Array: Read & Store Lines
One option is to use the readlines() method to read the contents of a file and store each line as an element in an array. Here is an example code:
# 打开文件
with open('file.txt', 'r') as file:
# 读取文件内容,并将每一行作为数组中的一个元素存储
content = file.readlines()
# 输出数组内容
for line in content:
print(line.strip())
In the code above, read the contents of the file file.txt line by line and store them in an array called content. Finally, output the file contents by iterating through the array.