如何用Python将元素写入数组

如何用Python将元素写入数组,python,arrays,string,Python,Arrays,String,我从txt文件中读取元素,我想将元素写入数组,我知道我必须使用子字符串方法,但我不知道在使用子字符串方法时如何生成数组 example.txt文件包括 001, A, 50, 70, 65 002, B, 25, 55, 80 003, C, 60, 40, 85 004, D, 75, 55, 70 005, E, 40, 40, 45 006, F, 35, 25, 85 我的python代码: file = open("example.txt", "r") a = file.read()

我从txt文件中读取元素,我想将元素写入数组,我知道我必须使用子字符串方法,但我不知道在使用子字符串方法时如何生成数组

example.txt文件包括

001, A, 50, 70, 65
002, B, 25, 55, 80
003, C, 60, 40, 85
004, D, 75, 55, 70
005, E, 40, 40, 45
006, F, 35, 25, 85
我的python代码:

file = open("example.txt", "r")
a = file.read()
print(a)

我需要生成30个元素多维5x6数组,我可以使用此代码读取此文件的元素,但我想知道如何将它们写入数组。

您需要将数据作为字符串读取,并在用逗号和换行符拆分数据时:

# Open file
with open("example.txt", "r") as f:
    # read the contents and replace "\n" characters by commas
    thedata = f.read().replace("\n", ",")
    # split it by "," creating an array
    array = thedata.split(",")
    # if it's needed, remove empty elements and trim spaces:
    array = [item.strip() for item in array if item.strip() != ""]
# Printing result:
print(array)
您所需要的只是以下内容:

它将返回我的列表,如下所示:


您可以这样做,使用split

用不同的方法展平列表

result = map(str.strip,[item for sublist in [i.split(',') for i in txt.split('\n')] for item in sublist])

周围有很多不错的基于理解的解决方案。这里有一个可供使用和:


要获得多维数组,您需要逐行读取文件,并用逗号分割每一行,就像在我的


如果您希望保留CSV文件的格式并拥有二维数组,那么可能首先设置二维数组,然后迭代文件并添加值

array = []

for row in range(0,6):
    array.append([])
    for col in range(0,5):
        array[row].append(" ")

print(array)

然后,要在中添加值而不是空格,请导入文件,遍历每一行,并为每一个值将其添加到2d数组中相应的空格中

看,您还应该考虑这些行。看起来是这样,但问题的作者说:我需要生成30个元素的数组,我已经回答了这个问题:我不认为如果你只使用逗号拆分,你就可以得到完整的30分;非常感谢,我还想把这些元素写成多维数组,我应该如何分割它们?@HüseyinBakan我已经为多维数组添加了答案。但是,列表展平的求和方法具有二次时间复杂性。您也没有剥离空间。@Schwobasegll还添加了剥离。您可能希望在关闭资源和限制其当前形式为Python2方面对其进行限定。这是最干净、最具python风格的方法!修好了。解决这个错误应该是你的家庭作业:为什么你有两个答案?@RoadRunner我的第一个答案是假设hbakan想要将所有数据读入一个列表。这是关于将数据加载到列表列表中。
In [14]: print map(str.strip,sum([i.split(',') for i in open("example.txt").split('\n')],[]))
['001', 'A', '50', '70', '65', '002', 'B', '25', '55', '80', '003', 'C', '60', '40', '85', '004', 'D', '75', '55', '70', '005', 'E', '40', '40', '45', '006', 'F', '35', '25', '85']
result = map(str.strip,[item for sublist in [i.split(',') for i in txt.split('\n')] for item in sublist])
from itertools import chain
with open("example.txt", "r") as f:
    array = list(chain(*(map(str.strip, line.split()) for line in f)))
# Prepare result array:
array = []
# Open file
with open("example.txt", "r") as f:
    # read the contents of file line by line:
    for line in f:
        # split current line by comma to get array of items from it
        array_parts = line.split(",")
        # filter elements, removing empty, and stripping spaces:
        array_parts = [item.strip() for item in array_parts if item.strip() != ""]
        # add line's items into result array:
        array.append(array_parts)        
# Printing result:
print(array)
array = []

for row in range(0,6):
    array.append([])
    for col in range(0,5):
        array[row].append(" ")

print(array)