Python reads csv files to excel,
My friend asked me how to use python to save a csv file as an xls file. I wanted to read the csv file and save it to the xls file. Maybe there are other simple methods, however, to practice python syntax and other knowledge, we use the following method. Here we make a record to help others.
# Coding: UTF-8 # import the corresponding module import csvimport xlwt # create an excel file myexcel = xlwt. workbook () # create a sheet page mysheet = myexcel. add_sheet ("testsheet") # open the csv file. It turns out that the file works the same as open. open csvfile = file ("test.csv", "rb") on the Internet ") # csvfile = open ("test.csv", "rb") # Read File Information reader = csv. reader (csvfile) l = 0 # obtain the information of a single row in a loop for line in reader: r = 0 # obtain the information of a single unit in a dual loop for I in line: print l, r # Write the mysheet in an excel table in a double loop. write (l, r, I) r = r + 1 l = l + 1 # Save it to excelmyexcel. save ("myexcel.xls ")