Python中文
首页
教程
问答
标签
搜索
登录
注册
如何在两列而不是两个数组中编写两个项目列表?
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我运行了我的代码,得到了如下两个列表:</p> <p><a href="https://i.stack.imgur.com/We5VZ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/We5VZ.jpg" alt="enter image description here"/></a></p> <p>我需要做的是把它们放在一个.csv文件中。 但我得到的结果看起来很难看:</p> <p><a href="https://i.stack.imgur.com/4utNo.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4utNo.jpg" alt="enter image description here"/></a></p> <p>正确的答案应该是这样的: <a href="https://i.stack.imgur.com/Y6vyu.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Y6vyu.jpg" alt="enter image description here"/></a></p> <p>那我该怎么解决这个问题呢</p> <p>代码:</p> <pre><code>from urllib.request import urlopen import urllib import re import csv def main(): html = urlopen("https://www.realestate.com.au/rent/with-1-bedroom-in-perth+-+greater+region%2c+wa/list-1?maxBeds=1&source=location-search").read().decode('utf-8') csvfile=open('price.csv','w',newline='') #### check ##print(price(html)) ##print(postcode(html)) #### end check writer=csv.writer(csvfile) writer.writerow(['Postcode','Price']) data=[price(html),postcode(html)] writer.writerows(data) csvfile.close() def price(page): priceTextReg=re.compile(r"priceText\">(.+?)<") priceText = re.findall(priceTextReg,page) p1=[] for i in range(20): ## we just need the first 20 items priceReg=re.compile(r"\d+") price=re.findall(priceReg,priceText[i]) p1.append(price[0]) return p1 def postcode(page): postcodeTextReg=re.compile(r"alt=.+?, WA (.+?)' title=") postcodeText=re.findall(postcodeTextReg,page) p2=[] for i in range(20): postcode=postcodeText[i] p2.append(postcode) return p2 </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>你会用字典吗<a href="https://docs.python.org/3/tutorial/datastructures.html" rel="nofollow noreferrer">https://docs.python.org/3/tutorial/datastructures.html</a> 将键设置为邮政编码,将值设置为价格</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何在python3x中以垂直和水平的方式格式化列表?
9 回答
如何在python3x中创建datetime对象
8 回答
如何在python3x中取消pickle一个在python2.7中被pickle的模型?
6 回答
如何在python3x中格式化字典键
5 回答
如何在python3x中正确地通过sockets发送字典的内容?
1 回答
如何在python3x中调用if语句中的类和函数
8 回答
如何在Python3上三取二列表
8 回答
如何在python3上为方法搜索RegEx创建模式?
9 回答
如何在Python3上使用HTMLUnit和Selenium 2.45.0
9 回答
如何在Python3上使用telebot发送没有bot.polling()的消息
5 回答
如何在python3上使用Tkinter更新GUI?
4 回答
如何在python3上使用urllib和代理对HTTPS页面发出请求
2 回答
如何在Python3上制作按键
1 回答
如何在python3上发布图像和文本多部分表单?
9 回答
如何在Python3上安装Bob?
6 回答
如何在python3上安装Django?
8 回答
如何在python3上安装第三方模块
9 回答
如何在Python3上安装袖扣?
5 回答
如何在python3上打印当前日期?
7 回答
如何在Python3上执行此javascript代码?
6 回答