python - open: integer is required 需要什么参数错误
问题描述
我正面临公开声明的问题。我不知道为什么期待一个整数
import urllib
site_url='https://en.wikipedia.org/wiki/Boroughs_of_New_York_City'
r = urllib.request.urlopen(site_url)
site_content = r.read().decode('utf-8')
with open('saved_page.html', 'w') as f:
f.write(site_content)
这是错误
TypeError Traceback (most recent call last)
<ipython-input-2-993aea7cff16> in <module>
12 site_content = r.read().decode('utf-8')
13
---> 14 with open('saved_page.html', 'w','utf-8') as f:
15 f.write(site_content)
TypeError: an integer is required (got type str)
解决方案
您将 'utf-8' 作为第三个位置参数传递。但是,当您查看文档时,您会发现这实际上是针对buffering
. 编码是在那之后。因此,您应该将其作为关键字参数传递:
with open('saved_page.html', 'w', encoding='utf-8') as f:
f.write(site_content)
推荐阅读
- jquery - jQuery:在通过 AJAX 为 Chosen JS 添加新选项之前删除未选择的选项
- python - 如何使用pydub?
- junit5 - 具有 jupiter-vintage-engine 规则的 Junit 5 Launcher 不支持 JUnit4 @ParametrizedTest
- r - R:ggplot2 直方图填充闪避 - 防止部分重叠
- ssl - Kubernetes 1.14.2 HA Master NGINX 负载均衡器 log.go:172] http:来自 192.168.5.32:43148 的 TLS 握手错误:远程错误:tls:证书错误
- spring-data-jpa - Spring-Data-JPA - 如何删除 OneToMany 关系中的子记录
- bash - bash 和 zsh 中的便携式数组索引
- usb - 仅基于 DESCRIPTOR HID 报告访问 USB 设备数据
- pandas - 如何使用额外的百分位数自定义熊猫盒和胡须图?
- python - 生成带有日期的随机时间序列数据