'application/octet-stream' 而不是 application/csv?
'application/octet-stream' instead of application/csv?
我对 Python 很陌生。我想确认数据集的类型(下面代码中的 URL)确实是一个 csv 文件。但是,当通过 headers 检查时,我得到 'application/octet-stream' 而不是 'application/csv'。
我假设我在读取数据时以错误的方式定义了一些东西,但我不知道是什么。
感谢您的帮助!
import requests
url="https://opendata.ecdc.europa.eu/covid19/casedistribution/csv/data.csv"
d1 = requests.get( url )
filePath = 'data/data_notebook-1_covid-new.csv'
with open(filePath, "wb") as f:
f.write(d1.content)
## data type via headers #PROBLEM
import requests
headerDict=d1.headers
#accessing content-type header
if "Content-Type" in headerDict:
print("Content-Type:")
print( headerDict['Content-Type'] )
I assume that I defined something in the wrong way when reading in the data
不,你没有。 Content-Type
header 是 应该 来指示响应 body 是什么,但是您无法强制服务器将其设置为您期望的值。有些服务器只是配置不当,无法正常运行。
application/octet-stream
是所有内容类型中最通用的内容类型 - 它只为您提供“这是一堆字节,玩得开心”的信息。
更重要的是,每种内容不一定有一种 True Type,只有 more-or-less 广泛 agreed-upon 约定。对于 CSV,常见的是 text/csv
.
因此,如果您确定内容是什么,请随时忽略 Content-Type header。
import requests
url = "https://opendata.ecdc.europa.eu/covid19/casedistribution/csv/data.csv"
response = requests.get(url)
filePath = 'data/data_notebook-1_covid-new.csv'
with open(filePath, "wb") as f:
f.write(response.content)
在没有任何进一步信息的情况下,以二进制模式写入文件是一个好主意,因为这将完全保留原始字节。
为了将其转换为字符串,需要使用特定的编码对其进行解码。由于 Content-Type 在这里没有给出任何指示(它可能会说 Content-Type: text/csv; charset=XYZ
),因此对于来自 Internet 的数据,最好的第一个假设是 UTF-8:
import csv
filePath = 'data/data_notebook-1_covid-new.csv'
with open(filePath, encoding='utf-8') as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
print(row)
如果结果是错误的(即存在解码错误或乱码),您可以尝试不同的编码,直到找到有效的编码。如果您一开始就以文本模式编写文件,那将是不可能的,因为错误解码造成的任何数据损坏都会进入文件。
我对 Python 很陌生。我想确认数据集的类型(下面代码中的 URL)确实是一个 csv 文件。但是,当通过 headers 检查时,我得到 'application/octet-stream' 而不是 'application/csv'。 我假设我在读取数据时以错误的方式定义了一些东西,但我不知道是什么。 感谢您的帮助!
import requests
url="https://opendata.ecdc.europa.eu/covid19/casedistribution/csv/data.csv"
d1 = requests.get( url )
filePath = 'data/data_notebook-1_covid-new.csv'
with open(filePath, "wb") as f:
f.write(d1.content)
## data type via headers #PROBLEM
import requests
headerDict=d1.headers
#accessing content-type header
if "Content-Type" in headerDict:
print("Content-Type:")
print( headerDict['Content-Type'] )
I assume that I defined something in the wrong way when reading in the data
不,你没有。 Content-Type
header 是 应该 来指示响应 body 是什么,但是您无法强制服务器将其设置为您期望的值。有些服务器只是配置不当,无法正常运行。
application/octet-stream
是所有内容类型中最通用的内容类型 - 它只为您提供“这是一堆字节,玩得开心”的信息。
更重要的是,每种内容不一定有一种 True Type,只有 more-or-less 广泛 agreed-upon 约定。对于 CSV,常见的是 text/csv
.
因此,如果您确定内容是什么,请随时忽略 Content-Type header。
import requests
url = "https://opendata.ecdc.europa.eu/covid19/casedistribution/csv/data.csv"
response = requests.get(url)
filePath = 'data/data_notebook-1_covid-new.csv'
with open(filePath, "wb") as f:
f.write(response.content)
在没有任何进一步信息的情况下,以二进制模式写入文件是一个好主意,因为这将完全保留原始字节。
为了将其转换为字符串,需要使用特定的编码对其进行解码。由于 Content-Type 在这里没有给出任何指示(它可能会说 Content-Type: text/csv; charset=XYZ
),因此对于来自 Internet 的数据,最好的第一个假设是 UTF-8:
import csv
filePath = 'data/data_notebook-1_covid-new.csv'
with open(filePath, encoding='utf-8') as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
print(row)
如果结果是错误的(即存在解码错误或乱码),您可以尝试不同的编码,直到找到有效的编码。如果您一开始就以文本模式编写文件,那将是不可能的,因为错误解码造成的任何数据损坏都会进入文件。