About the usage of csv.reader()

Reprinted from https://blog.csdn.net/swc5285018/article/details/78967958 
import json import csv import os def csv_process(filepath): with open(filepath , mode= 'r' , encoding= 'utf-8' , newline= '') as f: #The data read here is to return each line of data as a list reader = csv.reader(f) for row in reader: #The output is a list of rows at this time # print(row) print( '+'.join(row)) def json_process(filepath): with open(filepath , mode= 'r' , encoding= 'utf-8') as f: city_list = json.load(f) print(city_list) def main(): filepath = input( ' Please enter the file name: ') name ,index = os.path.splitext(filepath) if index == '.json': json_process(filepath) elif index == '.csv': csv_process(filepath) if __name__ == '__main__': main()
this is the csv file
time_point,aqi,quality,primary_pollutant,position_name,area,station_code,pm2_5,pm2_5_24h
2017-07-29T14:00:00Z,44, Excellent,, Temple of Heaven, Beijing,1004A,24,30
2017-07-29T14:00:00Z,46, Excellent,, Agricultural Exhibition Hall, Beijing,1005A,28,38 2017-07-29T14:00:00Z,47, Excellent,, Longevity West Palace, Beijing,1001A,32, 33 2017-07-29T14:00:00Z,50, Excellent,, Guocheng , Beijing,1012A,35,27 2017-07-29T14:00:00Z,54, Good, fine particles (PM2.5), million in Haidian District Liu, Beijing, 1007A, 38, 28 2017-07-29T14:00:00Z, 57, good, fine particles (PM2.5), East Fourth, Beijing, 1003A, 40, 36 2017-07-29T14:00:00Z ,58, good, fine particles (PM2.5), Guanyuan, Beijing,1006A,41,32 2017-07-29T14:00:00Z,58, good," particulate matter (PM2.5), particulate matter (PM10)" ,, Beijing,,40,29 2017-07-29T14:00:00Z,60, Good, fine particulate matter (PM2.5), Olympic Center, Beijing,1011A,43,32 2017-07-29T14:00:00Z ,63, Good,Particulate Matter (PM10), Dingling, Beijing, 1002A, 37, 20 2017-07-29T14:00:00Z, 77, Good, Particulate Matter (PM10), Changping Town, Beijing, 1010A, 46, 20 2017-07-29T14: 00:00Z, 80, good, ozone for 1 hour, Huairou Town, Beijing, 1009A, 48, 21 2017-07-29T14:00:00Z, 102, mild pollution, fine particulate matter (PM2.5), Shunyi New Town, Beijing ,1008A,76,38

 

reader = csv.reader(f) At this time, the value returned by the reader is a list of each line in the csv file, and the value read in each line is returned as a list
for row in reader:
    print(row)
At this point, the row printed by the running program is as follows:
['time_point', 'aqi', 'quality', 'primary_pollutant', 'position_name', 'area', 'station_code', 'pm2_5', 'pm2_5_24h']
['2017-07-29T14:00:00Z', '44', 'Excellent', '', 'Temple of Heaven', 'Beijing', '1004A', '24', '30']
['2017-07-29T14:00:00Z', '46', 'Excellent', '', 'Agricultural Exhibition Hall', 'Beijing', '1005A', '28', '38']
['2017-07-29T14:00:00Z', '47', 'Excellent', '', 'Longevity West Palace', 'Beijing', '1001A', '32', '33']
as a list
If we want to show it in another way, we can use the following method:
print(', '.join(row))
Shown as follows:
time_point,aqi,quality,primary_pollutant,position_name,area,station_code,pm2_5,pm2_5_24h
2017-07-29T14:00:00Z,44,Excellent,,Temple of Heaven,Beijing,1004A,24,30
2017-07-29T14:00:00Z,46,Excellent,,Agricultural Exhibition Hall,Beijing,1005A,28,38
2017-07-29T14:00:00Z,47,Excellent,,Longevity West Palace,Beijing,1001A,32,33
2017-07-29T14:00:00Z,50,Excellent,,Gucheng,Beijing,1012A,35,27
 
Here's a simple example of the above usage:
l = ['a','b','c','d']
print('+'.join(l))
 
The output is:
[a+b+c+d]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324771220&siteId=291194637