[英]How to encode list in python
I retrieve information from the web site using beautiful soup and I want to store these values in a single csv file row so i store this information in a list and later I store this list in the file. 我使用漂亮的汤从网站上检索信息,我想将这些值存储在单个CSV文件行中,因此我将此信息存储在一个列表中,然后再将此列表存储在文件中。 The problem is that I doesn't handle special characters.
问题是我不处理特殊字符。
#Répartition des prix moyen au m²
Repartition=[]
Inferieur=soup.find_all("text", {"class" : "p6_segmentMainLabel-outer"})
Superieur=soup.find_all("text", {"class" : "p6_segmentValue-outer"})
for Data in list(zip(Inferieur,Superieur)):
try:
Inferieur,Superieur = Data
Inferieur=Inferieur.string.encode('utf-8')
Superieur=Superieur.string.encode('utf-8')
Data2=' = '.join([Inferieur,Superieur])
print Data2
Repartition.append(Data2)
except NavigableString:
pass
After execution of the code I get : 执行代码后,我得到:
Supérieur à 12.27 €/m² = 14 Inférieur à 12.27 €/m² = 14
Supérieurà12.27€/m²= 14Inférieurà12.27€/m²= 14
here is the result in the csv file 这是csv文件中的结果
['Sup\\xc3\\xa9rieur \\xc3\\xa0 12.27 \\xe2\\x82\\xac/m\\xc2\\xb2 = 14', 'Inf\\xc3\\xa9rieur \\xc3\\xa0 12.27 \\xe2\\x82\\xac/m\\xc2\\xb2 = 14']
['Sup \\ xc3 \\ xa9rieur \\ xc3 \\ xa0 12.27 \\ xe2 \\ x82 \\ xac / m \\ xc2 \\ xb2 = 14','Inf \\ xc3 \\ xa9rieur \\ xc3 \\ xa0 12.27 \\ xe2 \\ x82 \\ xac / m \\ xc2 \\ xb2 = 14']
Help is very much appreciated! 非常感谢您的帮助!
If you want csv output, you should have a look at the csv
module - https://docs.python.org/3.6/library/csv.html?highlight=csv#csv.writer . 如果要csv输出,则应查看
csv
模块csv
? csv
= csv
。
Here's an example (taken from the Python docs and modified for your example): 这是一个示例(摘自Python文档,并针对您的示例进行了修改):
import csv
with open('output.csv', 'w', newline='') as csvfile:
w = csv.writer(csvfile, quotechar='|', quoting=csv.QUOTE_MINIMAL)
w.writerow(Repartition)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.