日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線(xiàn)溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
創(chuàng)新互聯(lián)Python教程:python中不同的CSV功能和使用

在之前的文章中介紹過(guò)為什么python學(xué)習(xí)中會(huì)使用CSV文件格式?這邊文章將會(huì)詳細(xì)介紹python中不同的CSV功能和使用。

成都創(chuàng)新互聯(lián)是一家專(zhuān)業(yè)提供防城企業(yè)網(wǎng)站建設(shè),專(zhuān)注與網(wǎng)站設(shè)計(jì)、做網(wǎng)站成都h5網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為防城眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。

一、CSV模塊功能

在CSV模塊下,可以找到以下功能

二、Python中CSV文件操作

加載CSV文件后,您可以執(zhí)行多種操作。將在Python中顯示對(duì)CSV文件的讀取和寫(xiě)入操作

在Python中讀取CSV文件:

import csv 
 
with open('Titanic.csv','r') as csv_file: #Opens the file in read mode
    csv_reader = csv.reader(csv_file) # Making use of reader method for reading the file
 
    for line in csv_reader: #Iterate through the loop to read line by line
        print(line)

用Python寫(xiě)入CSV文件:

import csv 
with open('Titanic.csv', 'r') as csv_file:
    csv_reader = csv.reader(csv_file) 
    with open('new_Titanic.csv', 'w') as new_file: # Open a new file named 'new_titanic.csv' under write mode
        csv_writer = csv.writer(new_file, delimiter=';') #making use of write method
 
        for line in csv_reader: # for each file in csv_reader
            csv_writer.writerow(line) #writing out to a new file from each line of the original file

讀取CSV文件作為字典

import csv 
 
with open('Titanic.csv','r') as csv_file: #Open the file in read mode
    csv_reader = csv.DictReader(csv_file) #use dictreader method to reade the file in dictionary
 
    for line in csv_reader: #Iterate through the loop to read line by line
        print(line)

作為字典寫(xiě)入CSV文件

import csv 
 
mydict = [{'Passenger':'1', 'Id':'0', 'Survived':'3'}, #key-value pairs as dictionary obj
          {'Passenger':'2', 'Id':'1', 'Survived':'1'},
          {'Passenger':'3', 'Id':'1', 'Survived':'3'}]
 
fields = ['Passenger', 'Id', 'Survived'] #field names
 filename = 'new_Titanic.csv' #name of csv file
 with open('new_Titanic.csv', 'w')as new_csv_file: #open a new file 'new_titanic,csv' under write mode
    writer = csv.DictWriter(new_csv_file, fieldnames=fields) 
    writer.writeheader() #writing the headers(field names)
 
    writer.writerows(mydict) #writing data rows

網(wǎng)頁(yè)題目:創(chuàng)新互聯(lián)Python教程:python中不同的CSV功能和使用
鏈接地址:http://m.5511xx.com/article/cophpeo.html