Python processes Data

Source: Internet
Author: User
Tags wkhtmltopdf

CSV data processing

CSV file format

Comma separators (CSV), sometimes referred to as character-delimited values, because delimited characters can also be not commas, their files store tabular data (numbers and text) in plain text form.
Plain text means that the file is a sequence of characters and does not contain data that must be interpreted like a binary number.
A CSV file consists of any number of records, separated by some sort of newline character, each record consists of a field, and the delimiter between the fields is another character or string, most commonly a comma or tab. Typically, all records have exactly the same sequence of fields.

CSV data format
27,20,14,15,14,12,94,64,37,1015,1013,1009,7,5,2,21,8,35,0.00,152

In addition, CSV files can be opened directly with Excel or similar software, and look like our common tabular form.

Common methods of reading data
ImportCodecslinetext=list () with Codecs.open ("Test.csv", encoding="Utf-8") as F: forLineinchf.readlines ():Print(Line.split (","))#prints the data for each row in a list format. Linetext.append (Line.split (","))    Print(Linetext)#put all the above rows as element data into a list. 

Working with CSV format data
Import Codecs Import  "test.csv"withCodecs.open (fileName) as fcsv:    = Csv.reader ( FCSV)    for in  linecsv]    print (rows)

Excel data Processing

Python provides third-party libraries to support Excel operations, and Python handles the third-party module libraries for Excel files, with Xlrd, XLWT, Xluntils, and Pyexcelerator, in addition to Python can also handle Excel with Win32com and OPENPYXL modules. We mainly use XLRD, XLWT, xluntils three modules, Pyexcelerator module occasionally will be used.

XLRD can    only read the Excel file, unable to write to the file, XLWT    can write to the file, but not in the existing Excel file modification; Xluntils can be modified on the existing    Excel file; Pyexcelerator    is similar to XLWT and can also be used to generate Excel files

Related Module installation
Pip Install xlrdpip install xlwtpip install XLUTILSPIP install Pyexcelerator

Reading table data by row
Importxlrddefreadexcel (): Data= Xlrd.open_workbook ('test.xlsx') Table= Data.sheets () [0]#Open the first sheetnrows = Table.nrows#get the number of rows in a table     forIinchRange (nrows):#Loop-by- line printing        Print(Table.row_values (i))#to get the value of each row by Row_values         if __name__=='__main__': Readexcel ()

Reading table data by column
Import= Xlrd.open_workbook ("whsc.xlsx"= Data.sheet_by_name ( " Domain name ")       # sheet name of the tab page  for inch Range (table2.ncols):     Print (Table2.col_values (COL))

Create an Excel file and write content
ImportXlwtexcel=XLWT. Workbook ()#Create 3 TablesSheet1 = Excel.add_sheet ("Sheet1") Sheet2= Excel.add_sheet ("Sheet2") Sheet3= Excel.add_sheet ("Sheet3")#write the data only in the first table Sheet1, as follows:Sheet1.write (0,0,"Hello world1", cell_overwrite_ok=True) Sheet1.write (1, 0,"Hello world2", cell_overwrite_ok=True) Sheet1.write (2, 0,"Hello world3", cell_overwrite_ok=True)#The first is the row, the second is the column, the third is the content, and the second parameter is used to confirm whether the same cell cell can reset the value. Excel.save ("hello.xlsx")Print("Create hello.xlsx Complete")

Use effects like styles, fonts, and more
ImportXlwtexcel=XLWT. Workbook ()#Create 3 TablesSheet1 = Excel.add_sheet ("Sheet1") Sheet2= Excel.add_sheet ("Sheet2") Sheet3= Excel.add_sheet ("Sheet3")#Initialize Stylestyle =XLWT. Xfstyle ()#Create a font for a styleFont =XLWT. Font () Font.Name='Times New Roman'   #Specify font nameFont.Bold = True#whether or not to add bold#set the font for a styleStyle.font =Font#Working with StylesSheet3.write (0,1,'some bold times text', Style)#Save the Excel file, overwriting the file with the same name directlyExcel.save ('hello.xlsx')Print('Create hello.xlsx file complete!')

Convert files into PDF format

At work, there are three ways to convert HTML files into PDF files and convert them into PDFs.
Python provides us with the Pdfkit module, which can be installed and used directly.

Install the module
Pip Install Pdfkit

Simple example
Import pdfkitpdfkit.from_file ("hello.html", 1.pdf)  # convert Web pages into PDFs (convert URLs directly to PDF files)Pdfkit.from_url ("www.baidu.com", 2.pdf)  # HTML converted to PDFpdfkit.from_string ("helloWorld"#  Convert strings into PDF

Grab a tutorial on Apelearn and replace it with a PDF
ImportOSImportReImportPdfkitImportRequestsif  notOs.path.exists ("Aminglinux"): Os.mkdir ("Aminglinux"# Create a directory to hold the generated PDF file Os.chdir ("Aminglinux"# Switch to the created directory URL.="http://www.apelearn.com/study_v2/"s=requests.session () text=s.get (URL). Textreg= Re.compile (r'<li class=\ "toctree-l1\" ><a class=\ "reference internal\" Href=\ "(. *) \" >.*<\/a><\/li> ') Result=reg.findall (text) Res=list (set (result)) forIinchRes:purl="{0}{1}". Format (URL, i)Print(purl) pdffilename= I.replace ("HTML","PDF")    Print(pdffilename) config= Pdfkit.configuration (wkhtmltopdf=r"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe")    Try: Pdfkit.from_url (purl, pdffilename, configuration=config)except:        Continue

Results:

Chapter1.pdfchapter2.pdfchapter3.pdfchapter4.pdfchapter5.pdf .......

Note: If you are using Windows to install a wkhtmltopdf driver, you will get an error.

Python processes Data

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.