测试基础 python 以字典方式读取 csv file

沐芓李 · September 26, 2021 · 1180 hits
def row_csv2dict(csv_file):
    try:
        dict_rows = []
        with open(csv_file)as f:
            reader = csv.reader(f)
            fieldnames = next(reader)

            index_length = len(fieldnames)
            for line in reader:
                data_row = {}
                for index in range(0, index_length):
                    data_row[fieldnames[index]] = line[index]

                dict_rows.append(data_row)

        return dict_rows

    except IOError:
        print("The file " + csv_file + " doesn't exist")
csv_file
user,password
test,test
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
No Reply at the moment.
需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up