python csv.reader 读取文件或list

发布时间 2023-04-18 19:13:51作者: panda4671

读取文件

with open(file_path, encoding='UTF-8') as file:
    lines = csv.reader(file, delimiter="#", quotechar='"')
    for row in lines:
        print(row)

  

读取list

注意:如果是字符串,一定要转成list. 例如 rows = csv.reader(["John#" #"Doe"# '21'])

import csv
csv_string = 'John,Doe,21\nJane,Smith,35\nAdam,Johnson,42'
rows = csv.reader(csv_string.splitlines())
first_row = next(rows)
print(first_row)