程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

List of various methods in Python -xls or CSV processing

編輯:Python

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

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 :

  • row - The row of the cell ( Index from 0 Start counting )
  • col - The column in which the cell is located ( Index from 0 Start counting )
  • *args - Additional parameters passed to the child method, such as numbers , character string , Format cells .

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_string()
  • write_number()
  • write_blank()
  • write_formula()
  • write_datetime()
  • write_boolean()
  • write_url()

write() The rules for processing data are as follows :

  • Data types such as float, int, long, decimal.Decimal and fractions.Fraction  Use write_number() Method .
  • Data types such as datatime.datetime,datetime.datedatetime.time  or  datetime.timedelta  Use write_datetime() Method .
  • None And empty string “” Use write_blank() Method .
  • data type bool Use write_boolean().

character string (String) Then process the data according to the following rules :

  • With “=” The initial matching formula string uses write_formula() Method . This method can be overridden , See below .
  • Match supported URL String using write_url() Method . This method can be overridden , See below .
  • In order to prevent Excel Warning “Numbers Stored as Text”, When Workbook() Constructors strings_to_numbers The options are True when , By float() Method to convert a string to a number using write_number() Method . See below .
  • Strings that do not match the above criteria will use write_string() Method .

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 ')

pandas Of to_csv One more column on the left

Set up index = False

csv For documents csv.writer Medium writerow Method write

Save multiple columns of data

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

There are extra blank lines

Add one more newline=''

with open(output_file, 'w+', newline='') as f:

String is divided into a character to occupy a cell

Use writerow Method must convert a string to a list , Otherwise, one character will occupy one cell .

So add [ ] that will do


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved