存儲(chǔ)數(shù)據(jù)基礎(chǔ)知識(shí).png
import csv
csv_file = open('demo.csv','w',newline='')
writer = csv.writer(csv_file)
writer.writerow(['電影','豆瓣評(píng)分'])
csv_file.close()
#csv讀取的代碼:
import csv
csv_file = open('demo.csv','r',newline='')
reader=csv.reader(csv_file)
for row in reader:
print(row)
#Excel寫入的代碼:
import openpyxl
wb = openpyxl.Workbook()
sheet = wb.active
sheet.title ='new title'
sheet['A1'] = '漫威宇宙'
rows = [['美國(guó)隊(duì)長(zhǎng)','鋼鐵俠','蜘蛛俠','雷神'],['是','漫威','宇宙', '經(jīng)典','人物']]
for i in rows:
sheet.append(i)
print(rows)
wb.save('Marvel.xlsx')
#Excel讀取的代碼:
import openpyxl
wb = openpyxl.load_workbook('Marvel.xlsx')
sheet = wb['new title']
sheetname = wb.sheetnames
print(sheetname)
A1_value = sheet['A1'].value
print(A1_value)