2023-10-10 python-读取txt文件中的约定格式的数据-记录

摘要:

2023-10-10 python-读取文件中的约定格式的数据-记录

txt文件中的内容:

[0196]=255,233,233
[0197]=252,221,221
[0198]=249,208,208
[0199]=247,196,196
[0200]=244,183,183
[0201]=241,171,171
[0202]=238,158,158
[0203]=235,146,146

读取内容的代码:

def read_color_txt(file_name, color_arr):
    f = open(file_name)

    line = f.readline()             
    while line:                  
        # print(line, end = '')
        line = line.replace("\n", "")
        line_split=line.split('=')
        # print(line_split[1])
        color_split=line_split[1].split(',')
        # print(color_split[0], color_split[1], color_split[2], end = '')
        color_px=[int(color_split[0]), int(color_split[1]), int(color_split[2])]
        color_arr.append(color_px)

你可能感兴趣的:(python,python,读取文本)