Python external Data read and write (Cvs,excel)

Source: Internet
Author: User

Python external Data Read and write

A practical Guide to Python Data science from Ji Lu

Csv
    • What is CSV?
      Comma-separated values (comma-separated values,csv, sometimes also called character-delimited values, because delimited characters can also be not commas)
    • CSV file read (str)
import csvwith open("D:/课件/《python数据科学》/data_science_tool_book_code/chapter7/test.csv") as fr:    rows = csv.reader(fr)    for row in rows:    #这里要缩进,rows相当于一个流,必须在文件开启时使用        print(row)        ######################[‘PassengerId‘, ‘Pclass‘, ‘Name‘, ‘Sex‘, ‘Age‘, ‘SibSp‘, ‘Parch‘, ‘Ticket‘, ‘Fare‘, ‘Cabin‘, ‘Embarked‘][‘892‘, ‘3‘, ‘Kelly, Mr. James‘, ‘male‘, ‘34.5‘, ‘0‘, ‘0‘, ‘330911‘, ‘7.8292‘, ‘‘, ‘Q‘][‘893‘, ‘3‘, ‘Wilkes, Mrs. James (Ellen Needs)‘, ‘female‘, ‘47‘, ‘1‘, ‘0‘, ‘363272‘, ‘7‘, ‘‘, ‘S‘][‘894‘, ‘2‘, ‘Myles, Mr. Thomas Francis‘, ‘male‘, ‘62‘, ‘0‘, ‘0‘, ‘240276‘, ‘9.6875‘, ‘‘, ‘Q‘][‘895‘, ‘3‘, ‘Wirz, Mr. Albert‘, ‘male‘, ‘27‘, ‘0‘, ‘0‘, ‘315154‘, ‘8.6625‘, ‘‘, ‘S‘][‘896‘, ‘3‘, ‘Hirvonen, Mrs. Alexander (Helga E Lindqvist)‘, ‘female‘, ‘22‘, ‘1‘, ‘1‘, ‘3101298‘, ‘12.2875‘, ‘‘, ‘S‘][‘897‘, ‘3‘, ‘Svensson, Mr. Johan Cervin‘, ‘male‘, ‘14‘, ‘0‘, ‘0‘, ‘7538‘, ‘9.225‘, ‘‘, ‘S‘]
    • CSV file read (dictionary)
import csvwith open(‘D:/课件/《python数据科学》/data_science_tool_book_code/chapter7/test.csv‘) as fr:    rows=csv.DictReader(fr)    for row in rows:        print(row)
    • CSV file Write
import csvwith open(‘./csv_tutorial.csv‘,‘a‘) as fw:    writer=csv.writer(fw)    writer.writerow([‘c1‘,‘c2‘,‘c3‘])   #同样,必须缩进
Excel
    • Pandas
      A powerful data processing library that includes the processing of Excel formats
    • Read Excel:
import pandas as pdfrom pandas import read_excelpd.set_option(‘display.max_columns‘,4)pd.set_option(‘display.max_rows‘,6)df= read_excel(‘D:/课件/《python数据科学》/data_science_tool_book_code/chapter8/A0202.xls‘,‘Sheet1‘)print(df)
    • Write Excel
import pandas as pdfrom pandas import DataFramedf=DataFrame([[1,2,3,4],[5,6,7,8],[9,10,11,12]],index=[0,1,2],columns=list("ABCD"))df.to_excel(‘test.xls‘)

Read and write Python external data (Cvs,excel)

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.