鱼C论坛

 找回密码
 立即注册
查看: 1875|回复: 3

请教大神,如何解决保存后的文件的乱码问题

[复制链接]
发表于 2018-3-18 12:37:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#!/usr/local/bin/python
#-*-coding:utf-8-*-
#Get market data from futures exchanges
#designed and programed by lianxiangbin(LianZhang,qq785674410)
#2014-10-21

import urllib.request
import urllib.parse
import json
import sqlite3
import datetime

class MarketDataFromSHFE:


    #define the attributes
    def __init__(self,date,SaveUrl):
        self.date=date[0:4]+date[5:7]+date[-2:];#"2014/05/23->20140523"
        self.url="http://www.shfe.com.cn/data/dailydata/kx/kx"+self.date+".dat";
        self.Data={};
        self.SaveUrl=SaveUrl;
  
    #get the html page
    def GetWebPage(self):
        MyPage=urllib.request.urlopen(self.url).read();#.decode("utf-8")
        if not MyPage:
            print("Dear Host,I can not find the web page");
        return MyPage;

    #this is the trading date
    def GetTheTradingDay(self):
        TradingDay=datetime.datetime(int(self.date[0:4]),int(self.date[4:6]),int(self.date[-2:]));
        return TradingDay;

    #Get the data we needed from the origin file
    def GetNeededData(self):
        MyPage=self.GetWebPage();
        MyPage=eval(MyPage);
        TradingDay=self.GetTheTradingDay();
        #print(MyPage);
        for dic in MyPage["o_curinstrument"]:
            key=dic["PRODUCTID"].strip()+dic["DELIVERYMONTH"];
            if (u"商品名称" in key) or (u"小计" in key) or (u"总计" in key):
                continue;
            Symbol=dic["PRODUCTID"].strip();
            Symbol=Symbol[0:2];
            values=[TradingDay,Symbol,dic["PRODUCTNAME"],dic["PRESETTLEMENTPRICE"],dic["OPENPRICE"],dic["HIGHESTPRICE"],dic["LOWESTPRICE"],dic["CLOSEPRICE"],\
                    dic["SETTLEMENTPRICE"],dic["ZD1_CHG"],dic["ZD2_CHG"],dic["VOLUME"],dic["OPENINTEREST"],dic["OPENINTERESTCHG"]];
            self.Data.setdefault(key,values);

    #Insert into DB
    def InsertDataToDB(self):
        self.GetNeededData();
        conna=sqlite3.connect(self.SaveUrl);
        #if conna:
        #    print("database is successfully connected");
        cursor=conna.cursor();
        SQLquery1="create table if not exists SHFE(Contracts varchar(20),date datetime,Symbol varchar(10),prodctname nvarchar(30),PreSettlement numeric(15,2),\
                  Open numeric(15,2),High numeric(15,2),Low numeric(15,2),Close numeric(15,2),Settlement numeric(15,2),Change1 numeric(15,2),\
                  Change2 numeric(15,2),Volume numeric(25,2),OpenInt numeric(25,2),ChangeofOpenInt numeric(25,2))";
        cursor.execute(SQLquery1);
        for key,value in self.Data.items():
            Iter=(key,value[0],value[1],value[2],value[3],value[4],value[5],value[6],value[7],value[8],value[9],value[10],value[11],value[12],value[13]);
            #print(Iter);
            SQLquery2="insert into SHFE"+" "+"values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            cursor.execute(SQLquery2,Iter);
        
        conna.commit();
        conna.close();

if __name__=="__main__":
    try:
        test=MarketDataFromSHFE("2018/03/16","商品有夜盘.csv");
        MyPage=test.InsertDataToDB();
    except Exception as e:
        print("something wrong");
        print(e);

我想用这段代码爬取数据并保存到csv文件中,但是却发现保存好的csv文件
是这幅模样,请教大神怎么才能解决这个问题
无标题.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-3-18 13:22:26 | 显示全部楼层
在编辑器上设置utf-8编码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-18 15:33:45 | 显示全部楼层
R9bot 发表于 2018-3-18 13:22
在编辑器上设置utf-8编码

感谢回复,具体是要怎么弄?是在哪里写uft-8
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-18 20:14:46 | 显示全部楼层
与道相融 发表于 2018-3-18 15:33
感谢回复,具体是要怎么弄?是在哪里写uft-8

你代码上不是写过utf-8了嘛,不用再写了。编辑器上一般都有一个选项是设置文件编码的,选上就好了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-12-28 16:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表