Python csv module, pythoncsv

Source: Internet
Author: User

Python csv module, pythoncsv

1 import csv # Comma Separated Value 2 3 # class Dialect 4 # delimiter = None # separator 5 # doublequote = None # The element is a quote, double the quote. The default value is true. If it is set to false, you need to set the delimiter. Otherwise, an error is returned. 6 # escapechar = None # Escape Character 7 # lineterminator = None # line terminator 8 # quotechar = None # reference character 9 # quoting = None # reference method 10 # skipinitialspace = None # ignore split the space behind, the default value is flase 11 #12 #13 # QUOTE_ALL = 1 # full quotation marks 14 # QUOTE_MINIMAL = 0 # delimiter and reference symbol plus double quotation marks 15 # QUOTE_NONE = 3 # All without double quotation marks, escape Character 16 # QUOTE_NONNUMERIC = 2 # number without quotation marks 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 und 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. Write a csv file quoting = csv. QUOTE_ALL 25 with open ("1.csv"," w ", newline =" ") as f: 26 # dialect is used to open a csv file. The default value is excel, the delimiter = "\ t" parameter indicates the delimiter 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 # insert A row of data into the csv file and put each item in the following list into A cell (multiple rows can be inserted cyclically) 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. Write A csv file quoting = csv. QUOTE_MINIMAL 44 with open ("2.csv"," w ", newline =" ") as f: 45 # dialect indicates how to open a csv file. The default value is excel, the delimiter = "\ t" parameter indicates the delimiter 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 # insert A row of data into the csv file and put each item in the following list into A cell (multiple rows can be inserted cyclically) 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. Write a csv file quoting = csv. QUOTE_NONE 64 with open ("3.csv"," w ", newline =" ") as f: 65 # dialect is used to open a csv file. The default value is excel, the delimiter = "\ t" parameter indicates the delimiter 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 = '\') # This method requires an escape character 69 # insert a row of data into the csv file, put each item in the following list into A cell (multiple rows can be inserted cyclically) 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. Write A csv file quoting = csv. QUOTE_NONNUMERIC 84 with open ("4.csv"," w ", newline =" ") as f: 85 # dialect is used to open a csv file. The default value is excel, delimiter = "\ t" parameter indicates the delimiter 86 # csv_writer = csv when writing. 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 # insert A row of data into the csv file and put each item in the following list into A cell (multiple rows can be inserted cyclically) 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", 5100 # "E", "F ","""", 6101 102 103 #5. Write a csv file quoting = default (quoting = csv. QUOTE_MINIMAL) 104 with open ("5.csv"," w ", newline =" ") as f: 105 # dialect is used to open a csv file. The default value is excel, the delimiter = "\ t" parameter indicates the delimiter 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 # insert a row of data into the csv file and put each item in the following list into a cell (multiple rows can be inserted in a loop) 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, ",", 5120 # E, F ,"""", 6121 122 #6. Write a csv file 123 with open ("6.csv"," w ", newline =" ") as f: 124 # dialect is used to open a csv file, the default value is excel. The delimiter = "\ t" parameter indicates the delimiter 126 # csv_writer = csv when writing. writer (f, dialect = "excel-tab") 127 # csv_writer = csv. writer (f, dialect = "excel", delimiter = "\ t") 128 csv_writer = csv. writer (f) 129 # insert A row of data into the csv file and put each item in the following list into A cell (multiple rows can be inserted cyclically) 130 csv_writer.writerow (["", "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, ",", 5140 # E, F, ", 6141 142 143 #7. Registration style 144 csv. register_dialect ('mystyle', delimiter = ',', quoting = csv. QUOTE_MINIMAL) 145 with open ("7.csv"," w ", newline =" ") as f: 146 # dialect is used to open a csv file. The default value is excel, the delimiter = "\ t" parameter indicates the delimiter 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 # insert a row of data into the csv file and put each item in the following list into a cell (multiple rows can be inserted cyclically) 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, ",", 5161 # E, F ,"""""""", 6162 163 164 #8. Read the csv file 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. Read the csv file 175 for row in csv. reader (['one, two, three ', 'one, two, three'], skipinitialspace = True): # skipinitialspace ignore space 176 print (row, type (row )) 177 # ['one', 'two', 'three '] <class 'LIST'> 178 # ['one', 'two ', 'Three '] <class 'LIST'> 179 180 181 #10, DictWriter182 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': 'bak', '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. DictReader193 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', 'bak'), ('Last _ name ', 'beans ')]) 197 print (row ['first _ name'], row ['last _ name'])

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.