python3基礎 讀取和寫入csv檔案

2021-08-20 21:07:37 字數 1361 閱讀 6869

['city', 'password', 'day']

['1', '1', '1']

['2', '1', '1']

['3', '1', '1']

['4', '1', '1']

['5', '1', '1']

import csv

#開啟乙個csv檔案,模式為讀取

csvfile = open('/users/yanghui/study/python/example.csv','r')

#定義乙個變數,進行讀取

readcsv = csv.reader(csvfile)

print(readcsv)

for row in readcsv:

print(row)

readcsv = csv.reader(csvfile)

rows = [row for row in readcsv]

print(rows[0][1])

readcsv = csv.reader(csvfile)

for i,rows in enumerate(readcsv):

if i == 0:

row = rows

print(rows[1])

readcsv = csv.reader(csvfile)

list1 =

list2 =

list3 =

for row in readcsv:

print(list1)

print(list2)

print(list3)

1

import

csv2

#開啟檔案,用with開啟可以不用去特意關閉file了,python3不支援file()開啟檔案,只能用open()

3 with open("

***.csv

","r

",encoding="

utf-8

") as csvfile:4#

讀取csv檔案,返回的是迭代型別

5 read =csv.reader(csvfile)

6for i in

read:

7print(i)

寫入csv檔案

import csv

#開啟乙個csv檔案,模式為寫,如果沒有該檔案,則建立乙個

with open('/users/yanghui/study/python/test.csv','a') as csvfile:

#定義乙個寫變數

writecsv = csv.writer(csvfile)

writecsv.writerow(['index','yanghui','18'])

Python3中讀取寫入csv檔案

import csv 讀取資料 file path d 1csvread.csv with open file path,encoding utf 8 as f reader csv.reader f result list reader reader 方法返回物件,需要先轉型成list才能看到資料...

Python3 寫入csv檔案

import json import codecs res res json.dumps res,ensure ascii false res str res f codecs.open read.csv a encoding gbk f.write res f.close 1 使用codecs將包...

python3讀取網頁

網上用python讀取網頁的介紹很多,但是因為版本的問題,總是弄不對,這裡就介紹下python3讀取網頁的步驟,較少,只是為了與python2 相區別 urlopen url 另外,python中的製表符如下 在需要在字元中使用特殊字元時,python用反斜槓 轉義字元。如下表 原始字串 有時我們並...