pyhon3.5 打开csv文件中文乱码问题

将编码格式改成gb18030就ok了


# -*- coding: utf-8 -*-
"""
Created on Wed Aug  2 16:56:32 2017

@author: lengxia
"""

import urllib.request
import re
import csv
import codecs
import sys
from bs4 import BeautifulSoup 
import importlib
wiki = 'https://zh.wikipedia.org/wiki/%E6%96%87%E4%BB%B6%E7%BC%96%E8%BE%91%E5%99%A8%E6%AF%94%E8%BE%83'
header = {'User-Agent': 'Mozilla/5.0'}
page = urllib.request.urlopen(wiki).read()
page=page.decode("utf8")
soup = BeautifulSoup(page,"lxml")
name = ""       #名字
creater = ""    #归属
first = ""      #首次公开发布的时间
latest = ""     #最新稳定版本
cost = ""       #售价
licence = ""    #授权条款
table = soup.find("table", {"class" : "sortable wikitable"})
f=open('url2table.csv', 'w',encoding="gb18030")
csv_writer = csv.writer(f)


td_th = re.compile('t[dh]')

for row in table.findAll("tr"):
    cells = row.findAll(td_th)
    if len(cells) == 6:
        name = cells[0].find(text=True)
        if not name:
            continue
        creater = cells[1].find(text=True)
        first = cells[2].find(text=True)
        latest = cells[3].find(text=True)
        cost = cells[4].find(text=True)
        licence = cells[5].find(text=True)

        print([ x for x in [name, creater, first, latest, cost, licence]])
        csv_writer.writerow([ x for x in [name, creater, first, latest, cost, licence]])

f.close()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值