Detailed summary of python reading and writing csv files

  • 2021-07-09 08:55:05
  • OfStack

python provides a large number of libraries, which can be very convenient for various operations. Now, the method of reading and writing csv files in python is presented in a procedural way.

When writing python program, csv module or pandas module is needed, in which csv module does not need to be downloaded and installed again, and pandas module needs to follow the corresponding

python version installation.

You can install pandas in an python2 environment by:


sudo pip install pandas

You can install pandas in an python3 environment by:


sudo pip3 install pandas

1. Summary of reading and writing csv files with csv

When reading the file, open the file and call csv. reader () to read the file; For the contents of the read file, you can input them into another file for saving by traversing every 1 line of the read file and then writing them to the specified file using csv_write. writerow ().

2. Read and write csv file sample code using csv


#encoding:utf-8
import csv
 
# Read csv Documents 
csv_file = csv.reader(open('../../data/capital/2010-Q4-cabi-trip-history-data.csv'))
print(csv_file)
 
# Add newline Can be avoided 1 Space after line , This needs to be done in python3 Run under the environment 
out = open('../../data/capital/2010-Q4','a',newline='')
csv_write = csv.writer(out,dialect='excel')
 
for item in csv_file:
	#print item
	csv_write.writerow(item)
 
print("write over")

The above code is the traditional way to open a file-read a file-open a file-close a file to read and write files. The reader () and writer () of the csv library are called in the process of reading and writing, respectively

3. Read and write csv file sample code using csv


#encoding:utf-8
import csv
 
# The following one is added encoding The writing of needs to be in python3 Run under the environment 
csv_reader = csv.reader(open('../../data/capital/2011-Q1-cabi-trip-history-data.csv',encoding='utf-8'))
 
out = open('../../data/capital/a0','w',newline='')
csv_writer = csv.writer(out,dialect='excel')
 
 
for row in csv_reader:
	# Every output 1 OK yes 1 A list,list Every one in 1 Elements are converted to string Type 
	print(row)
		csv_writer.writerow(row)

The reading and writing mode of Sample Code 2 is basically the same as that of Sample Code 1. The difference is that the encoding mode is specified when opening the file in Sample Code 2, and the opening mode of the output file is also a little different from that of Sample Code 1. The right 'a' is changed to 'w

4. Read and write csv file sample code using csv


#encoding:utf-8
import csv
 
# Read csv File mode 1
csvFile = open('../../data/capital/2011-Q3-cabi-trip-history-data.csv','r')
reader = csv.reader(csvFile)
 
data = []
 
for item in reader:
#	print(item)
	data.append(item)
 
#print(data)
 
csvFile.close()
 
 
# Read csv File mode 2
with open("../../data/capital/2011-Q3-cabi-trip-history-data.csv",'r') as csvFile:
	# Read csv Documents , Returns the iteration type 
	reader2 = csv.reader(csvFile)
	for item2 in reader2:
		print(item2)
csvFile.close()
 
 
# Write from list csv Documents 
# Settings newline, Otherwise, there will be an empty space between the two lines 1 Row 
csvFile2 = open('../../data/capital/0001.csv','w',newline='')
writer = csv.writer(csvFile2)
m = len(data)
for i in range(m):
	writer.writerow(data[i])
csvFile2.close()
 
 
# Write from dictionary csv Documents 
dic = {' Zhang 3':123,' Li 4':456,' Wang 2 Baby ':789}
csvFile3 = open('../../data/capital/0001.csv','w',newline='')
writer2 = csv.writer(csvFile3)
for key in dic:
	print(key)
#	writer2.writerow([key,dic[key]])
 
csvFile3.close()

In the code above, two different ways to open an csv file and write an csv file are presented. The first opening mode is very similar to the previous two, except that in this mode, the reading mode is added when opening the file, while the previous two sample codes are not added. Another way to read is to use the unique naming method in python to name the open file, and other parts are similar. In this example, a distinction is made between list and dictionary writing to the csv file, providing sample code for list and dictionary writing to the csv file, respectively.

5. Read and write csv file sample code using csv


#encoding:utf-8
'''
import pandas as pd
# Arbitrary multi-group list 
a = [1,2,3]
b = [4,5,6]
# In a dictionary key The value is csv Column name in 
dataFrame = pd.DataFrame({'a_name':a,'b_name':b})
# Will DataFrame Store as csv,index Indicates whether the line name is displayed ,default=True
dataFrame.to_csv('0001.csv',index=False,sep='')
#pandas Read provided csv Method of 
data = pd.read_csv('test.csv')
'''
 
 
#===================================
# Another 1 Methods : Use csv Bag ,1 Row 1 Row writing 
import csv
 
#python2 Can be used file Substitution open
with open('test.csv','w') as csvFile:
	writer = csv.writer(csvFile)
	# Write first columns_name
	writer.writerow(["index","a_name","b_name"])
	# Writing multiple lines with writerows
	writer.writerows([[1,2,3],[0,1,2],[4,5,6]])
 
# Use reder Read csv Documents 
with open('test.csv','r') as csvFile:
	reader = csv.reader(csvFile)
	for line in reader:
		print line

This sample code proposes the implementation of writing 1 line and writing multiple lines.

6. Read and write csv file sample code using csv


#encoding:utf-8
import csv
 
 
with open('test.csv') as csvFile:
	#readcsv = csv.reader(csvFile,delimiter='')
	readcsv = csv.reader(csvFile)
	#
	rows= [row for row in readcsv]
	for row in readcsv:
		rows= [row for row in reader]
		#print(row)# Print 1 Row 
		print(row[0])# Print 1 One in the row cell
		print(row[0],row[1])# Print 1 One in the row cell
 
print('=================')
 
with open('test.csv') as csvFile:
	readCSV = csv.reader(csvFile)
	cols1 = []
	cols2 = [] 
	cols3 = []
	for row in readCSV:
		col1 = row[0]	
		col2 = row[1]
		col3 = row[2]
		
		cols1.append(col1)
		cols2.append(col2)
		cols3.append(col3)
 
print cols1
print cols2
print cols3

The above code example suggests a new way to read all rows, a way to read an cell and a 1 column.

7. Read and write csv file sample code using csv


#encoding:utf-8
import csv
 
# No. 1 1 Methods : Use reader Function , Receive 1 Iterable objects ( For example csv Documents ), Be able to return 1 Generator , You can parse it out of it csv Content of .eg: The following code can be read csv The whole content of , In behavioral units 
with open('test.csv','rb') as csvFile:
	reader = csv.reader(csvFile)
	rows = [row for row in reader]
 
print rows
 
 
print('===========')
 
# Extract one of them 1 Column can use the following methods 
with open('test.csv','rb') as csvFile:
	reader = csv.reader(csvFile)
	column = [row[1] for row in reader]
print column
 
 
 
print('====== No. 1 2 Methods ======')
# No. 1 2 Methods : Use DictReader, And reader Function similarity , Receiving Iterable Objects , Be able to return 1 Iterable objects , Be able to return 1 Generator , But every returned 1 Cells are placed in 1 Within the value of a dictionary , And the key of this dictionary is the title of this cell . You can see with the following code DictReader Structure of :
with open('test.csv','rb') as csvFile:
	reader = csv.DictReader(csvFile)
	column = [row for row in reader]
print(column)
 
# Use CictReader Read csv Of a 1 Column , This allows you to query by column heading 
with open('test.csv','rb') as csvFile:
	reader = csv.DictReader(csvFile)
	column = [row['a_name'] for row in reader]
print column

The above code proposes a new way to read whole rows and columns. In addition, a way of reading by column name using DictReader is proposed.

8. Read and write csv file sample code using csv


#encoding:utf-8
import csv
 
with open('test.csv','rb') as csvFile:
	reader = csv.reader(csvFile)
	for row in reader:
		print row
 
# Toward csv Write content in file , Writing like this will write all the contents to 1 A cell Medium 
with open('test1.csv','wb') as csvFile:
	writer = csv.writer(csvFile,delimiter=' ',quotechar='|',quoting=csv.QUOTE_MINIMAL)
	writer.writerow(['a','4','5','6'])
	writer.writerow(['b','1','2','3'])
	writer.writerow(['c','9','8','7'])
	writer.writerow(['d','8','3','1'])
 
 
# Toward csv Write content in file , The following writing can finally put every 1 The contents of the column are placed in the 1 A cell Medium 
with open('test2.csv','wb') as csvFile:
	writer = csv.writer(csvFile,dialect='excel')
	writer.writerow(['a','4','5','6'])
	writer.writerow(['b','1','2','3'])
	writer.writerow(['c','9','8','7'])
	writer.writerow(['d','8','3','1'])

The above code suggests a way to put the data in the list into different cell implementations.


Related articles: