Python data format-CSV

CSV file: Comma-Separated Values, Chinese name, comma-separated value or character-separated value, its file store table data in the form of plain text. The file is a sequence of characters, which can consist of any number of records, separated by some kind of newline character. Each record is composed of fields, and the separator between fields is other characters or strings. All records have exactly the same sequence of fields, which is equivalent to the plain text form of a structured table.
CSV files can be opened with text files, Excel or similar and text files.

Write to CSV

import csv #Need to import library
withopen('data.csv','w')as fp:
 writer = csv.writer(fp)#Pass in the file handle first
 writer.writerow(['id','name','age'])#Then write
 writer.writerow(['10001','mike','20'])#Write by line
 writer.writerow(['10002','Bob','22'])
 writer.writerow(['10003','Jordan','21'])

import csv #Need to import library
withopen('data.csv','w')as fp:
 writer = csv.writer(fp,delimiter ='*')#delimiter can only be a one-byte character
 writer.writerow(['id','name','age'])#Then write
 writer.writerow(['10001','mike','20'])#Write by line
 writer.writerow(['10002','Bob','22'])
 writer.writerow(['10003','Jordan','21'])

import csv
withopen('data.csv','w')as fp:
 fieldnames =['id','name','age']  #First define the key in the dictionary
 # Use DictWriter() method to add a fieldnames
 writer = csv.DictWriter(fp,fieldnames  = fieldnames,delimiter ='+') 
 writer.writeheader()#Write key first
 # Write in the dictionary
 writer.writerow({'id':'10001','name':'mike','age':'20'})
 writer.writerow({'id':'10002','name':'Bob','age':'22'})
 writer.writerow({'id':'10003','name':'Jordan','age':'21'})

Read CSV

  1. The first
import csv
withopen('data.csv','r',encoding ='utf8')as fp:
 reader = csv.reader(fp)for row in reader:print(row)
  1. The second
import pandas as pd #Need to import pandas library
df = pd.read_csv('data.csv')print(df)

Recommended Posts

Python data format-CSV
02. Python data types
Python data model
Python data analysis
python data structure
Python data analysis-data update
Python data analysis-apply function
Python data analysis-data selection
Python basic data types
Python basic data types
Python data analysis-data establishment
Python Data Science: Neural Networks
Python common data structure collation
Python3 crawler data cleaning analysis
Python parses simple XML data
Python Data Science: Logistic Regression
Python Data Science: Regularization Methods
Python Data Science: Related Analysis
Python Data Science: Linear Regression
Python Faker data forgery module
Python Data Science: Chi-Square Test
Python Data Science: Linear Regression Diagnosis
Is python suitable for data mining
Python multithreading
Python CookBook
Python FAQ
Python3 dictionary
Python3 module
python (you-get)
Automatically generate data analysis report with Python
Python string
Python access to npy format data examples
Python basics
Python descriptor
Python basics 2
Python exec
Python notes
Python3 tuple
CentOS + Python3.6+
Python advanced (1)
Python decorator
Python IO
Python multithreading
Python toolchain
Python3 list
Python multitasking-coroutine
Python overview
python introduction
Python analytic
Python basics
07. Python3 functions
Java or Python for big data analysis
Python uses pandas to process Excel data
Python functions
python sys.stdout
python operator
Python entry-3
Centos 7.5 python3.6
Python string
python queue Queue
Python basics 4