
在读取文件时候比如读取 xxx.csv 时候 可能报编码错误
类似于
1 | 'xxx' codec can't decode byte 0xac in position 211: illegal multibyte sequen
|
1 2 3 4 5 6 7 8 | id_list = []
with open('E:/work_spider/xxx/xx.csv', "r", encoding="utf-8") as csvfile:
csvReader = csv.reader(csvfile)
for content in csvReader:
content = str(content)
if 'l.' in content:
continue
id_list.append(content.split('\\')[0].replace("['", ""))
|
可以在读取时候尝试制定编码方式。
保存图片或者视频文件的时候或许也会报错 Unicode decode error xxxxxxxxxxx
1 2 3 | VideoHtmlContent = requests.get(url = VideoUrl,headers=headers).content
with open('bobovideo.mp4','wb',) as f:
f.write(VideoHtmlContent)
|
不要忘记 它的文件打开方式 图片可视频都是以bytes类型二进制方式请求和写入 我们使用‘wb’,以二进制写模式打开
open有很多打开模式 以下仅供参考和查阅:
a表示append,r表示read,w表示write,+表示读写模式。,b表示二进制,t表示文本模式,t是默认的模式。
1 2 3 4 5 6 7 8 9 10 11 | w 以写方式打开,
a 以追加模式打开 (从 EOF 开始, 必要时创建新文件)
r+ 以读写模式打开
w+ 以读写模式打开
a+ 以读写模式打开
rb 以二进制读模式打开
wb 以二进制写模式打开
ab 以二进制追加模式打开
rb+ 以二进制读写模式打开
wb+ 以二进制读写模式打开
ab+ 以二进制读写模式打开
|