惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
Jina AI
Jina AI
Microsoft Security Blog
Microsoft Security Blog
IT之家
IT之家
V
Visual Studio Blog
量子位
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 聂微东
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
大猫的无限游戏
大猫的无限游戏
Y
Y Combinator Blog
Stack Overflow Blog
Stack Overflow Blog
D
Docker
MyScale Blog
MyScale Blog
小众软件
小众软件
云风的 BLOG
云风的 BLOG
美团技术团队
Microsoft Azure Blog
Microsoft Azure Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 【当耐特】

博客园 - zzuCharles

Agent Bug 为什么不可复现 Qwen-Max 8G 内存本地部署方案(轻量化可用版) MFC中使用多线程 高性能 Flink SQL 优化 Flink 时间概念 EventTime 和 Watermark SQL 优化(来源平时总结及网络分享) hadoop 查询优化 文件拆分-python Mysql 调优笔记 Python 输出菱形 Python 读取目录,office文件分类 Python 猜数小程序(练习) Mysql 字符串日期互转 MaxCompute 语句笔记 数据仓库架构 Python 比较两个字符串的相似度 Python print Python简单计算器 用户价值模型 CITE :https://www.jianshu.com/p/34199b13ffbc 用户生命周期模型
python 读取csv多编码兼容读取
zzuCharles · 2021-04-30 · via 博客园 - zzuCharles
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 30 09:19:24 2021

@author: charles
"""

import chardet
import csv


def read_csv(filename):
    encodings = ['gbk','utf-8','utf-8-sig','GB2312','gb18030',]
    for e in encodings:
        data = []
        try:
            with open(filename, encoding=e) as f:
                reader = csv.reader(f)
                #print(reader)
                #header = next(reader)
                #print(header)
                for row in reader:
                    data.append(row)
                    print(row)        
            print(filename,e)
            return data
        except:
            print(filename,e)
    #print(filename,"==================")



if __name__ == '__main__':
    path = "C:/Users/charles/Desktop/1.csv"
    f = open(path,'rb')
    data = f.read()
    print(chardet.detect(data))
    df=read_csv(path)