Catalog
xlsxwriter
pandas Of to_csv One more column on the left
csv For documents csv.writer Medium writerow Method write
Save multiple columns of data
There are extra blank lines
String is divided into a character to occupy a cell
xlsxwriter This module , It generates files with the suffix .xlsx, It can support 1048576 Row data ,16384 Column data
wirte(row, col, *args)
Write normal data to worksheet cells .
Parameters :
Excel Distinguish between different data types such as strings , Numbers , Space , Formulas and hyperlinks . In order to simplify XlsxWriter The process of writing data from a file , write() Method as a pseudonym for the following specific methods ( translator's note : This means that programmers are usually not required to explicitly specify the following methods , In the use of write() When the method is used XlxsWriter It will judge the data type according to the rules and write the data in the corresponding method ):
write() The rules for processing data are as follows :
character string (String) Then process the data according to the following rules :
If the value does not match all of the above types , Will eventually use float() To check whether it corresponds to the user-defined floating-point type . If not , Will use write_number() Method .
Last , If none of these rules match , Then the program will throw TypeError abnormal .
Example
import xlsxwriter
# Write excel
def write_excel():
workbook = xlsxwriter.Workbook('chat.xlsx')# Create a excel file
worksheet = workbook.add_worksheet(u'sheet1')# Create a file named TEST Of sheet, Without a name, the default is sheet1
worksheet.set_column('A:A',20)# Set the width of the first column to 20 Pixels
bold= workbook.add_format({'bold':True})# Set a bold format object
worksheet.write('A1','HELLO')# stay A1 Cell write HELLO
worksheet.write('A2','WORLD',bold)# stay A2 It says WORLD, And set to bold
worksheet.write('B2',U' Chinese test ',bold)# stay B2 It is written in bold Chinese
worksheet.write(2,0,32)# Write numbers in rows and columns 32,35,5
worksheet.write(3,0,35.5)# When using rows and columns, the first line starts with 0, therefore 2,0 Represents the first column of the third row , Equivalent to A4
worksheet.write(4,0,'=SUM(A3:A4)')# write excel The formula
workbook.close()
if __name__ == '__main__':
# write in Excel
write_excel();
print (' Write successfully ')
Set up index = False
with open("gduf.csv",'a',encoding="utf-8",newline="") as f:
writer=csv.writer(f)# Get the write object first
writer.writerow([title.a.text,title_url])# Write two columns of data
Add one more newline=''
with open(output_file, 'w+', newline='') as f:
Use writerow Method must convert a string to a list , Otherwise, one character will occupy one cell .
So add [ ] that will do