我是初学者不太懂
为什么在终端显示是正确的顺序到了csv 文件中就是另一回事了呢
还有就是 csv 文件怎么可以运行之后继续填写 而不是清空文件呢?
图片:


代碼:
import urllib.request
import re
import bs4
import csv
from bs4 import BeautifulSoup
url="http://10.104.65.9/home/part/shuiQing.jsp"
data=urllib.request.urlopen(url).read()
data=data.decode('UTF-8')
soup=BeautifulSoup(data,"html.parser")
foundtxt=soup.find_all('td',height='22')
rol=[]
index=0
#with open(r'C:\Users\skyb52\Desktop\12.csv','wb') as csvfile:
with open(r'C:\Users\skyb52\Desktop\12.csv','w',newline='') as csvfile:
spamwriter = csv.writer(csvfile,dialect='excel')
for i in foundtxt:
rol.append(i.string)
index=index+1
if index==3:
spamwriter.writerow({rol[0],rol[1],rol[2]})
print(rol[0],rol[1],rol[2])
rol=[]
index=0
csvfile.close()
最后添加上 内网网站源代码:
站名
水位(m)
流量(m3/s)
小浪底
134.63
565
花园口
89.05
445
夹河滩
72.58
400
高村
58.98
360
孙口
44.29
358
艾山
36.82
225
泺口
25.7
207
利津
9.36
76.5
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
你寫到 csv 的代碼:
你寫了一個 set 出來,他是無序的,而你
print的時候是有序的:結果自然不同
不清空而是附加新的資料上去的作法,在於使用
open打開文檔時,要使用'a'(append) 模式:改成: