python + csv operation (read and write)

CSV Import 

"" "
with different excel file, csv file:
1. Data are no data type, the value is 'string'
2. No color and style, can not specify the width and height of the test cell, the cell can not merge
3 . no work table
4. The image can not be embedded chart
"" "
readcsv.csv follows:

 

 

Data = # [] 
# = csvFile Open ( 'readcsv.csv', 'R & lt')
# = csv.reader Reader (csvFile)
# Item for in Reader:
# # Print (Item)
# data.append (Item)
# Print (Data)
#
# # Close csv file
# csvFile.close ()

#
# read local csv file
# with Open ( 'readcsv.csv', 'R & lt') AS csvFile:
# = csv.reader Reader1 (csvFile)
# for in Reader1 Line:
# Print (Line)

csvFile2.csv as follows:

 



# Csv file is written from the list -> Read data from the list (a)
# = csvFile2 Open ( 'csvFile2.csv', 'W', NEWLINE = '', encoding = 'UTF-. 8')
# Writer csv.writer = (csvFile2)
# m = len (Data)
# for I in Range (m):
# writer.writerow (Data [I])
# csvFile2.close ()

# write csv file from the list -> read from the data list (B)
DATAl = [[ 'A1', 123], [ 'A2', 234], [ 'A3', 345], [ 'A4', 456]]
csvFile2 = Open ( 'csvFile2 .csv ',' W ', NEWLINE =' ', encoding =' UTF-. 8 ')
Writer = csv.writer (csvFile2)
m = len (DATAl)
for I in Range (m):
writer.writerow (DATAl [I ])
csvFile2.close ()

# written from the dictionary
data2 = { 'b1': 123 , 'b2': 234, 'b3': 345, 'b4':456}
csvFile2 = open('csvFile2.csv','w',newline='',encoding='utf-8')
writer = csv.writer(csvFile2)
for key in data2:
writer.writerow([key,data2[key]])
csvFile2.close()


Guess you like

Origin www.cnblogs.com/Teachertao/p/11892381.html