如何将文本文件放入元组中的二维列表?

时间:2017-04-28 02:18:50

标签: python list tuples 2d

我遇到了将文本文件传递到二维元组数组的问题。这是我的输入文件的样子,它非常大,所以这只是它的一部分。

26 54 94 25 53 93 24 52 92 25 53 93 25 53 93 25 53 93 25 53 93 27 55 95 28 55 98 26 53 96 25 52 95 26 53 96 27 54 97 28 55 98 27 54 97 26 53 96 26 55 97 26 55 97 26 55 97 26 55 97 25 54 96 25 54 96 25 54 96 26 55 97 26 55 99 27 56 100 28 57 101 26 55 99 25 54 98 26 55 99 26 55 99 26 55 99 25 54 98 26 55 99 27 56 100 27 56 100 26 55 99 26 55 99 26 55 99 27 56 100 28 57 101 29 58 102 29 58 102

这是读取文件并将其放入二维数组

的函数
def load_image_data(infile):
    '''
    Accepts an input file object
    Reads the data in from the input PPM image into a 2-dimensional list of RGB tuples
    Returns the 2-dimensional list
    '''
    print("Loading image data...\n")

    for line in infile.readlines():
        line = line.strip()
        values = line.split(" ")
        new_line = []
        for j in range(int(len(new_line) / 3) + 1):
            for i in range(len(new_line) // 3):
                r = new_line[0]
                g = new_line[1]
                b = new_line[2]
                t = (r, g, b)

                t_list.append(t)
                del new_line[0]
                del new_line[0]
                del new_line[0]

                new_line.append(t)
        print(new_line)
        print("done")
        return new_line`

这是main

def main():
    '''
    Runs Program
    '''

    mods = ["vertical_flip", "horizontal_flip", "horizontal_blur", "negative",
            "high_contrast", "random_noise", "gray_scale", "remove_color"]
            # ** finish adding string modifications to this list
    for mod in mods:

        # get infile name
        #file = (input("Please enter the input file name: "))

        # get outfile name
        #out = (input("Please enter the output file name: "))

        infile = open("ny.ppm", "r") # ** get the filename from the user
        outfile = open("ny_negative.ppm", "w") # ** change to use mod and user-spec filename

        process_header(infile, outfile)
        load_image_data(infile)
        process_body(infile, outfile, mod)

        outfile.close()
        infile.close()

3 个答案:

答案 0 :(得分:0)

将文件中的数据作为inf读取,并将其拆分以获取数据列表。然后,遍历范围的项目(number_of_items // 3),然后将所需的长度附加到列表中并返回相同的内容!

print("Loading image data...\n")

inf=infile.readlines()
inf = inf[0].split()
new_line=[]
for i in range(len(inf)//3):
    r,b,g=inf[i*3:i*3+3]
    print r,g,b
    t = (r, g, b)
    t_list.append(t)
    new_line.append(inf[i*3:i*3+3])
return new_line

在你的主要()

infile = open("ny.ppm", "r") # ** get the filename from the user
print load_image_data(infile)
infile.close()

示例输出:

[['26', '54', '94'], ['25', '53', '93'], ['24', '52', '92'], ['25', '53', '93'], ['25', '53', '93'], ['25', '53', '93'], ['25', '53', '93'], ['27', '55', '95'], ['28', '55', '98'], ['26', '53', '96'], ['25', '52', '95'], ['26', '53', '96'], ['27', '54', '97'], ['28', '55', '98'], ['27', '54', '97'], ['26', '53', '96'], ['26', '55', '97'], ['26', '55', '97'], ['26', '55', '97'], ['26', '55', '97'], ['25', '54', '96'], ['25', '54', '96'], ['25', '54', '96'], ['26', '55', '97'], ['26', '55', '99'], ['27', '56', '100'], ['28', '57', '101'], ['26', '55', '99'], ['25', '54', '98'], ['26', '55', '99'], ['26', '55', '99'], ['26', '55', '99'], ['25', '54', '98'], ['26', '55', '99'], ['27', '56', '100'], ['27', '56', '100'], ['26', '55', '99'], ['26', '55', '99'], ['26', '55', '99'], ['27', '56', '100'], ['28', '57', '101'], ['29', '58', '102'], ['29', '58', '102']]

希望它有所帮助!

答案 1 :(得分:0)

以下是你如何组成元组:

In [8]: from itertools import islice

In [9]: with open("yourfile.DATA") as f:
    ...:    data = f.read().split()
    ...:    size = len(data)
    ...:    it = map(int, data)
    ...:    data = [tuple(islice(it,0,3)) for _ in range(0, size, 3)]
    ...:

输出:

In [10]: data
Out[10]:
[(26, 54, 94),
 (25, 53, 93),
 (24, 52, 92),
 (25, 53, 93),
 (25, 53, 93),
 (25, 53, 93),
 (25, 53, 93),
 (27, 55, 95),
 (28, 55, 98),
 (26, 53, 96),
 (25, 52, 95),
 (26, 53, 96),
 (27, 54, 97),
 (28, 55, 98),
 (27, 54, 97),
 (26, 53, 96),
 (26, 55, 97),
 (26, 55, 97),
 (26, 55, 97),
 (26, 55, 97),
 (25, 54, 96),
 (25, 54, 96),
 (25, 54, 96),
 (26, 55, 97),
 (26, 55, 99),
 (27, 56, 100),
 (28, 57, 101),
 (26, 55, 99),
 (25, 54, 98),
 (26, 55, 99),
 (26, 55, 99),
 (26, 55, 99),
 (25, 54, 98),
 (26, 55, 99),
 (27, 56, 100),
 (27, 56, 100),
 (26, 55, 99),
 (26, 55, 99),
 (26, 55, 99),
 (27, 56, 100),
 (28, 57, 101),
 (29, 58, 102),
 (29, 58, 102)]

列表理解可以更详细地写成:

In [11]: with open('yourfile.DATA') as f:
    ...:    data = f.read().split()
    ...:    size = len(data)
    ...:    it = map(int, data)
    ...:    data = []
    ...:    for _ in range(0, size, 3):
    ...:        data.append(tuple(islice(it, 0, 3)))
    ...:

请注意,我使用了with块,在处理文件时是可取的,它们不仅会为您关闭文件,还会使确定文件已关闭(在例如,甚至是异常处理的情况。

一条建议,小心地传递文件处理程序。当你做这样的事情时:

infile = open("ny.ppm", "r") # ** get the filename from the user
outfile = open("ny_negative.ppm", "w") # ** change to use mod and user-spec filename

process_header(infile, outfile)
load_image_data(infile)
process_body(infile, outfile, mod)

outfile.close()        
infile.close()

请注意像infile这样的文件处理程序有点像单遍迭代器,并且只能执行.readlines()之类的操作。因此,如果您在infile.readlines()中使用process_header,当您将同一infile传递给process_body时,对infile.readlines()的后续调用将引发错误,除非您重置文件光标显式使用infile.seek(0) - 这就是为什么我说它们是"有点"像一个迭代器。但我建议不要处理它,而是传递一个文件路径的字符串,并使用with - 块打开您的文件。

答案 2 :(得分:0)

这样的东西会将图像数据作为元组列表读取(并返回):

[[(255, 0, 0), (0, 255, 0), (0, 0, 255), ...], 
 [(255, 255, 0), (255, 255, 255), (0, 0, 0), ...],
    ...
]

输出格式示例:

state.id = state.id + 1;
state.list.push(state.id);