python 操作csv文件写入顺序不对

查看:191
本文介绍了python 操作csv文件写入顺序不对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

我是初学者不太懂

  1. 为什么在终端显示是正确的顺序到了csv 文件中就是另一回事了呢

  2. 还有就是 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()  


最后添加上 内网网站源代码:

<style type="text/css">
<!--
.style1 {
    font-size:12px;
}
-->
</style>
<body topmargin="0" leftmargin="0">
<table width="244" border="0" cellpadding="0" cellspacing="1" bgcolor="#ffffff" class="style1">
  <tr>
    <td  height="24" align="center" bgcolor="#b8b8b8" width="30%">站名</td>
    <td  align="center" bgcolor="#b8b8b8" width="33%">水位(m)</td>
    <td  align="center" bgcolor="#b8b8b8">流量(m<sup>3</sup>/s)</td>
  </tr>
  
  <tr>
<td height="22" align="center" bgcolor="#eaeaea">小浪底</td>
<td height="22" align="center" bgcolor="#eaeaea">134.63</td>
<td height="22" align="center" bgcolor="#eaeaea">565</td>
</tr>
<tr>
<td height="22" align="center" bgcolor="#FFFFFF">花园口</td>
<td height="22" align="center" bgcolor="#FFFFFF">89.05</td>
<td height="22" align="center" bgcolor="#FFFFFF">445</td>
</tr>
<tr>
<td height="22" align="center" bgcolor="#eaeaea">夹河滩</td>
<td height="22" align="center" bgcolor="#eaeaea">72.58</td>
<td height="22" align="center" bgcolor="#eaeaea">400</td>
</tr>
<tr>
<td height="22" align="center" bgcolor="#FFFFFF">高村</td>
<td height="22" align="center" bgcolor="#FFFFFF">58.98</td>
<td height="22" align="center" bgcolor="#FFFFFF">360</td>
</tr>
<tr>
<td height="22" align="center" bgcolor="#eaeaea">孙口</td>
<td height="22" align="center" bgcolor="#eaeaea">44.29</td>
<td height="22" align="center" bgcolor="#eaeaea">358</td>
</tr>
<tr>
<td height="22" align="center" bgcolor="#FFFFFF">艾山</td>
<td height="22" align="center" bgcolor="#FFFFFF">36.82</td>
<td height="22" align="center" bgcolor="#FFFFFF">225</td>
</tr>
<tr>
<td height="22" align="center" bgcolor="#eaeaea">泺口</td>
<td height="22" align="center" bgcolor="#eaeaea">25.7</td>
<td height="22" align="center" bgcolor="#eaeaea">207</td>
</tr>
<tr>
<td height="22" align="center" bgcolor="#FFFFFF">利津</td>
<td height="22" align="center" bgcolor="#FFFFFF">9.36</td>
<td height="22" align="center" bgcolor="#FFFFFF">76.5</td>
</tr>

  
</table>
</body>

解决方案

你寫到 csv 的代碼:

spamwriter.writerow({rol[0],rol[1],rol[2]})

你寫了一個 set 出來,他是無序的,而你 print 的時候是有序的:

print(rol[0],rol[1],rol[2])

結果自然不同


不清空而是附加新的資料上去的作法,在於使用 open 打開文檔時,要使用 'a' (append) 模式:

with open(r'C:\Users\skyb52\Desktop\12.csv','w',newline='') as csvfile:
                                            ^^^
                                       寫入模式,會覆蓋掉原本的資料從頭開始寫入

改成:

with open(r'C:\Users\skyb52\Desktop\12.csv','a',newline='') as csvfile:
                                            ^^^
                                       附加模式,會從文件最後開始寫入

这篇关于python 操作csv文件写入顺序不对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆