Recently used CSV file , So let's summarize CSV Read write operation of file

1. write in CSV file :




import csv     # need import csv Package for

out=open("aa.csv",'wb')         # Notice here if ‘w’ Open in the form of , There will be a blank line in the middle of each data write , So use ‘wb’

csv_write=csv.write(out,dialect='excel')

                                              # Write the specific content below

ss=['a',3]

csv_write.writerow(ss)      # such ss The information is written into aa.csv In the file , And it's in two columns

                                     # Here, if you need to write multiple lines , So it's OK to use cycle input

out.close()




2. read CSV file

import csv

out=open("aa.csv",'r')

read_csv=csv.reader(out,dialect='excel')

for line in read_csv:     # Cycle output csv All data in

    print line

out.close()




It's still very simple , ha-ha