11.python-list saved in csv format

Background:
The list data generated during the running of the program is saved as a csv table to the local.

import csv

import pandas as pd

list = [[1, 2, 3], [4, 5, 6], [7, 9, 9]]
name = ['one', 'two', 'three']
name2 = ['小花', '小明', '小智']
test = pd.DataFrame(columns=name, index=name2, data=list)
print(test)
# 转换csv
test.to_csv('testcsv.csv', encoding='gbk')

# 读取CSV文件--csv库
with open('testcsv.csv','r') as f:
    reader = csv.reader(f);
    # print(reader)
    for row in reader:
        print(row)

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/u014217137/article/details/128206147