1 import csv # Comma Separated Value 2 3 # class Dialect 4 # delimiter = None # 分隔符 5 # doublequote = None # 元素是引用符,双倍的引用符。默认是true,如果改为false就需要设置分隔符,否则报错。 6 # escapechar = None # 转义符 7 # lineterminator = None # 行终止符 8 # quotechar = None # 引用符 9 # quoting = None # 引用方式 10 # skipinitialspace = None # 忽略分割后面的空格,默认是flase 11 # 12 # 13 # QUOTE_ALL = 1 # 全加引号 14 # QUOTE_MINIMAL = 0 # 分隔符和引用符号加双引号 15 # QUOTE_NONE = 3 # 全部不加双引号,分隔符要加转义符 16 # QUOTE_NONNUMERIC = 2 # 数字不加引号 17 # 18 # csv.QUOTE_MINIMAL means only when required, for example, when a field contains either the quotechar or the delimiter 19 # csv.QUOTE_ALL means that quotes are always placed around fields. 20 # csv.QUOTE_NONNUMERIC means that quotes are always placed around fields which do not parse as integers or floating point numbers. 21 # csv.QUOTE_NONE means that quotes are never placed around fields. 22 23 24 # 1、写一个csv文件 quoting=csv.QUOTE_ALL 25 with open("1.csv", "w", newline="") as f: 26 # dialect为打开csv文件的方式,默认是excel,delimiter="\t"参数指写入的时候的分隔符 27 # csv_writer = csv.writer(f, dialect="excel-tab") 28 # csv_writer = csv.writer(f, dialect="excel", delimiter="\t") 29 csv_writer = csv.writer(f, dialect="excel", quoting=csv.QUOTE_ALL) 30 # csv文件插入一行数据,把下面列表中的每一项放入一个单元格(可以用循环插入多行) 31 csv_writer.writerow(["A", "B", ",", 5]) 32 csv_writer.writerow(["E", "F", ‘"‘, 6]) 33 34 with open("1.csv", "rb") as f: 35 print(f.read()) 36 # b‘"A","B",",","5"\r\n"E","F","""","6"\r\n‘ 37 38 with open("1.csv", "r") as f: 39 print(f.read()) 40 # "A","B",",","5" 41 # "E 42 43 # 2、写一个csv文件 quoting=csv.QUOTE_MINIMAL 44 with open("2.csv", "w", newline="") as f: 45 # dialect为打开csv文件的方式,默认是excel,delimiter="\t"参数指写入的时候的分隔符 46 # csv_writer = csv.writer(f, dialect="excel-tab") 47 # csv_writer = csv.writer(f, dialect="excel", delimiter="\t") 48 csv_writer = csv.writer(f, dialect="excel", quoting=csv.QUOTE_MINIMAL) 49 # csv文件插入一行数据,把下面列表中的每一项放入一个单元格(可以用循环插入多行) 50 csv_writer.writerow(["A", "B", ",", 5]) 51 csv_writer.writerow(["E", "F", ‘"‘, 6]) 52 53 with open("2.csv", "rb") as f: 54 print(f.read()) 55 # b‘A,B,",",5\r\nE,F,"""",6\r\n‘ 56 57 with open("2.csv", "r") as f: 58 print(f.read()) 59 # A,B,",",5 60 # E,F,"""",6 61 62 63 # 3、写一个csv文件 quoting=csv.QUOTE_NONE 64 with open("3.csv", "w", newline="") as f: 65 # dialect为打开csv文件的方式,默认是excel,delimiter="\t"参数指写入的时候的分隔符 66 # csv_writer = csv.writer(f, dialect="excel-tab") 67 # csv_writer = csv.writer(f, dialect="excel", delimiter="\t") 68 csv_writer = csv.writer(f, dialect="excel", quoting=csv.QUOTE_NONE, escapechar=‘\\‘) # 这种方式需要设置转义符 69 # csv文件插入一行数据,把下面列表中的每一项放入一个单元格(可以用循环插入多行) 70 csv_writer.writerow(["A", "B", ",", 5]) 71 csv_writer.writerow(["E", "F", ‘"‘, 6]) 72 73 with open("3.csv", "rb") as f: 74 print(f.read()) 75 # b‘A,B,\\,,5\r\nE,F,\\",6\r\n‘ 76 77 with open("3.csv", "r") as f: 78 print(f.read()) 79 # A,B,\,,5 80 # E,F,\",6 81 82 83 # 4、写一个csv文件 quoting=csv.QUOTE_NONNUMERIC 84 with open("4.csv", "w", newline="") as f: 85 # dialect为打开csv文件的方式,默认是excel,delimiter="\t"参数指写入的时候的分隔符 86 # csv_writer = csv.writer(f, dialect="excel-tab") 87 # csv_writer = csv.writer(f, dialect="excel", delimiter="\t") 88 csv_writer = csv.writer(f, dialect="excel", quoting=csv.QUOTE_NONNUMERIC) 89 # csv文件插入一行数据,把下面列表中的每一项放入一个单元格(可以用循环插入多行) 90 csv_writer.writerow(["A", "B", ",", 5]) 91 csv_writer.writerow(["E", "F", ‘"‘, 6]) 92 93 with open("4.csv", "rb") as f: 94 print(f.read()) 95 # b‘"A","B",",",5\r\n"E","F","""",6\r\n‘ 96 97 with open("4.csv", "r") as f: 98 print(f.read()) 99 # "A","B",",",5 100 # "E","F","""",6 101 102 103 # 5、写一个csv文件 quoting=默认 (quoting=csv.QUOTE_MINIMAL) 104 with open("5.csv", "w", newline="") as f: 105 # dialect为打开csv文件的方式,默认是excel,delimiter="\t"参数指写入的时候的分隔符 106 # csv_writer = csv.writer(f, dialect="excel-tab") 107 # csv_writer = csv.writer(f, dialect="excel", delimiter="\t") 108 csv_writer = csv.writer(f, dialect="excel") 109 # csv文件插入一行数据,把下面列表中的每一项放入一个单元格(可以用循环插入多行) 110 csv_writer.writerow(["A", "B", ",", 5]) 111 csv_writer.writerow(["E", "F", ‘"‘, 6]) 112 113 with open("5.csv", "rb") as f: 114 print(f.read()) 115 # b‘A,B,",",5\r\nE,F,"""",6\r\n‘ 116 117 with open("5.csv", "r") as f: 118 print(f.read()) 119 # A,B,",",5 120 # E,F,"""",6 121 122 123 # 6、写一个csv文件 124 with open("6.csv", "w", newline="") as f: 125 # dialect为打开csv文件的方式,默认是excel,delimiter="\t"参数指写入的时候的分隔符 126 # csv_writer = csv.writer(f, dialect="excel-tab") 127 # csv_writer = csv.writer(f, dialect="excel", delimiter="\t") 128 csv_writer = csv.writer(f) 129 # csv文件插入一行数据,把下面列表中的每一项放入一个单元格(可以用循环插入多行) 130 csv_writer.writerow(["A", "B", ",", 5]) 131 csv_writer.writerow(["E", "F", ‘"‘, 6]) 132 133 with open("6.csv", "rb") as f: 134 print(f.read()) 135 # b‘A,B,",",5\r\nE,F,"""",6\r\n‘ 136 137 with open("6.csv", "r") as f: 138 print(f.read()) 139 # A,B,",",5 140 # E,F,"""",6 141 142 143 # 7、注册风格 144 csv.register_dialect(‘mystyle‘, delimiter=‘,‘, quoting=csv.QUOTE_MINIMAL) 145 with open("7.csv", "w", newline="") as f: 146 # dialect为打开csv文件的方式,默认是excel,delimiter="\t"参数指写入的时候的分隔符 147 # csv_writer = csv.writer(f, dialect="excel-tab") 148 # csv_writer = csv.writer(f, dialect="excel", delimiter="\t") 149 csv_writer = csv.writer(f, dialect="mystyle") 150 # csv文件插入一行数据,把下面列表中的每一项放入一个单元格(可以用循环插入多行) 151 csv_writer.writerow(["A", "B", ",", 5]) 152 csv_writer.writerow(["E", "F", ‘"""‘, 6]) 153 154 with open("7.csv", "rb") as f: 155 print(f.read()) 156 # b‘A,B,",",5\r\nE,F,"""""""",6\r\n‘ 157 158 with open("7.csv", "r") as f: 159 print(f.read()) 160 # A,B,",",5 161 # E,F,"""""""",6 162 163 164 # 8、读取csv文件 165 csv.register_dialect(‘mystyle‘, delimiter=‘,‘, quoting=csv.QUOTE_MINIMAL) 166 with open(‘7.csv‘, newline=‘‘) as f: 167 reader = csv.reader(f, ‘mystyle‘) 168 for r in reader: 169 print(r, type(r)) 170 # [‘A‘, ‘B‘, ‘,‘, ‘5‘] <class ‘list‘> 171 # [‘E‘, ‘F‘, ‘"‘, ‘6‘] <class ‘list‘> 172 173 174 # 9、读取csv文件 175 for row in csv.reader([‘one, two, three‘, ‘one, two, three‘], skipinitialspace=True): # skipinitialspace忽略空格 176 print(row, type(row)) 177 # [‘one‘, ‘two‘, ‘three‘] <class ‘list‘> 178 # [‘one‘, ‘two‘, ‘three‘] <class ‘list‘> 179 180 181 # 10、DictWriter 182 with open(‘8.csv‘, ‘w‘, newline=‘‘) as csv_file: 183 fieldnames = [‘first_name‘, ‘last_name‘] 184 writer = csv.DictWriter(csv_file, fieldnames=fieldnames) 185 186 writer.writeheader() 187 writer.writerow({‘first_name‘: ‘Baked‘, ‘last_name‘: ‘Beans‘}) 188 writer.writerow({‘first_name‘: ‘Lovely‘, ‘last_name‘: ‘Spam‘}) 189 writer.writerow({‘first_name‘: ‘Wonderful‘, ‘last_name‘: ‘Spam‘}) 190 writer.writerows([{‘first_name‘: ‘Wonderful‘, ‘last_name‘: ‘Spam‘}, {‘first_name‘: ‘Wonderful‘, ‘last_name‘: ‘Spam‘}]) 191 192 # 11、DictReader 193 with open(‘8.csv‘, newline=‘‘) as csv_file: 194 reader = csv.DictReader(csv_file) 195 for row in reader: 196 print(row) # OrderedDict([(‘first_name‘, ‘Baked‘), (‘last_name‘, ‘Beans‘)]) 197 print(row[‘first_name‘], row[‘last_name‘])