CSV文件的基本操作

# hanbb
# come on!!!
import csv

# 文件的整体读取
examplefile= open('E:\downloades.txt')    # 打开文件
examplereader = csv.reader(examplefile)     # 读取文件  注意不能直接访问文件名字,必须先打开
examplerdata = list(examplereader)          # 将返回为内容变成列表 操作

# 文件的逐行读取
openfile = open('E:\downloades.txt')      # 先打开
readfile = csv.reader(openfile)             # 读取
#for row in readfile:                        # 循环读取
    #print("row # " + str(readfile.line_num)+ ' ' +str(row))

# 文件的写入
file = open('E:\downloadwriter.csv','w',newline='')  # 打开的文件名称,写入模式,不写newline=''会出现行间距变大
writerfile = csv.writer(file)                          # 写入命令
writerfile.writerow(['name','address','city','state']) # 写入内容
writerfile.writerow(['hbb','hangzhou','hangzhou','china'])
file.close()                                           # 关闭文件
原文地址:https://www.cnblogs.com/hanbb/p/7891854.html