from urllib.request import urlopen
from io import StringIO
import csv
data = urlopen("http://pythonscraping.com/files/MontyPythonAlbums.csv").read().decode('ascii', 'ignore')
dataFile = StringIO(data)
# csvReader = csv.reader(dataFile)
# for row in csvReader:
# print(row)
dictReader = csv.DictReader(dataFile)
print(dictReader.fieldnames)
for row in dictReader:
print(row)
import csv
# 读取 CSV 文件
file = open('test.csv')
reader = csv.reader(file)
data = list(reader)
print(data)
print(data[0][0])
# 写入 CSV 文件
file = open('test.csv', 'w', newline='')
writer = csv.writer(file)
writer.writerow(['spam', 'eggs'])
writer.writerow(['spam', 'eggs'])
writer = csv.writer(file, delimiter='\t', lineterminator='\n\n')
writer.writerow(['apple', 'banana'])
writer.writerow(['apple', 'banana'])
file.close()
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于