python 打开csv文件,报错'utf-8' codec can't decode bytes in position 16: invalid continuation byte

问题:

with open(1.csv, 'r', encoding='utf-8'):
    ori_lines = f.readlines()
    ......

运行报错:

'utf-8' codec can't decode bytes in position 15-16: invalid continuation byte

原因

csv中的编码格式是‘ANSI’,而Python 3默认读取格式为‘utf-8’,导致decode异常。

解决方案:

打开csv文件 --> 另存为,选择其他格式,右下角工具中选择web选项,编码中的格式选择Unicode(UTF-8):
在这里插入图片描述
在这里插入图片描述
这样之后就解决错误了~

### 洛谷 P5714 题目解析 对于洛谷平台上的P5714题目,该题旨在考察字符串处理以及简单的逻辑判断能力。此题要求实现的功能是对输入的一个整数n (0 ≤ n < 2^31),将其转换成英文单词表示形式并输出。 #### 实现方法概述 为了完成这一任务,可以采用分组的方式处理数字到文字的转化过程。具体来说: - 将整个数值按照千位分割为多个部分; - 对每一部分单独进行翻译; - 使用预定义好的映射表来辅助快速定位对应的英语表达方式; 下面给出完整的C语言解决方案[^1]。 ```c #include <stdio.h> #include <string.h> const char* lessThan20[] = {"", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ", "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen "}; const char* tens[] = {"", "", "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "}; const char* thousands[] = {"", "Thousand, ", "Million, ", "Billion, "}; void helper(int num, char *result){ int hundred = num / 100; int remainder = num % 100; if(hundred >= 1){ strcat(result, lessThan20[hundred]); strcat(result,"Hundred "); } if(remainder >= 20){ strcat(result,tens[remainder/10]); strcat(result,lessThan20[remainder%10]); }else{ strcat(result,lessThan20[remainder]); } } char* numberToWords(int num) { if(num == 0)return "Zero"; static char result[100]=""; memset(result,'\0',sizeof(result)); int i=0; do{ if(num%1000>0){ char tempResult[50]; memset(tempResult,'\0',sizeof(tempResult)); helper(num%1000,tempResult); strcat(strcat(tempResult,thousands[i]),result); strcpy(result,tempResult); } ++i; }while((num/=1000)>0); //去除最后一个多余的逗号和空格 result[strlen(result)-2]='\0'; return result; } ``` 上述代码实现了将任意正整数转化为相应英文表述的功能。其中`helper()`函数负责处理小于一千的部分,而主函数则控制整体流程,并调用`helper()`依次处理每个三位一组的数据片段。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值