Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python读取生成HTTPError_Python_Pandas - Fatal编程技术网

Python读取生成HTTPError

Python读取生成HTTPError,python,pandas,Python,Pandas,这可能是一个新手问题,但我无法理解它,需要帮助了解正在发生的事情。我想从URL的文本(.txt)文件中检索数据,并将数据放入Pandasdataframe中进行进一步操作。由于csv\u read允许我从URL检索数据,这是我的默认选择,但由于某种原因,我得到了一个HTTPError 400(错误请求)异常。因此,我尝试使用Request模块从相同的URL检索相同的文件,该模块运行良好。(对我来说)奇怪和莫名其妙的是,csv_read命令将毫无例外地执行,只要请求检索在它之前。我无法理解为什么单

这可能是一个新手问题,但我无法理解它,需要帮助了解正在发生的事情。我想从URL的文本(.txt)文件中检索数据,并将数据放入
Pandas
dataframe中进行进一步操作。由于
csv\u read
允许我从URL检索数据,这是我的默认选择,但由于某种原因,我得到了一个HTTPError 400(错误请求)异常。因此,我尝试使用
Request
模块从相同的URL检索相同的文件,该模块运行良好。(对我来说)奇怪和莫名其妙的是,
csv_read
命令将毫无例外地执行,只要
请求
检索在它之前。我无法理解为什么单独执行
csv\u read
命令时会引发异常,但是如果我在前面使用另一个模块执行了完全独立的代码段,那么同一段代码就会正常执行,没有任何异常

显然,我可以使用
request
检索文件,然后将数据传递到
pandas
dataframe,但我不愿意在不了解原因的情况下绕过这个问题。我已经在下面的代码中加入了一些注释来说明问题。我在macOS上

import requests
import pandas as pd

## This is the file I want to retrieve
link = 'https://seb.se/pow/fmk/2100/fonder_2019-10-17.TXT'

## 1. Retrieves the file/data via requests. Works fine.
f = requests.get(link)
print(f.text)

## 2. Retrieves the file via csv_read. This part will execute fine if the first segment runs before it, but if I comment out the two lines under 1 this code will throw a HTTPError 400 (Bad Request.
c = pd.read_csv(link, encoding='iso-8859-1', header=None, sep=';', names=["Date","Fund","NAV","ID"])
print(c.head())
异常回溯:

Traceback (most recent call last):
  File "/Users/mac/Documents/seb_fonder_filer.py", line 13, in getFile
    c = pd.read_csv(link, header=None, sep=';', encoding='ISO-8859-1', names=["Date","Fund","NAV","ID"])
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/io/parsers.py", line 685, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/io/parsers.py", line 439, in _read
    fp_or_buf, _, compression, should_close = get_filepath_or_buffer(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/io/common.py", line 196, in get_filepath_or_buffer
    req = urlopen(filepath_or_buffer)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 531, in open
    response = meth(req, response)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 640, in http_response
    response = self.parent.error(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 569, in error
    return self._call_chain(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 502, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request

很奇怪,对我来说很好。。。我使用的是
panda==0.24.0
requests==2.22.0
你确定是panda的csv返回http400错误吗?我使用的是
panda==0.25.3
requests==2.22.0
加上python3.8@朱尔斯,你也试过对
请求
检索进行注释吗?我可以直接调用它,没有其他导入:
pd.read\u csv('https://seb.se/pow/fmk/2100/fonder_2019-10-17.TXT,encoding='iso-8859-1',header=None,sep=';',names=[“日期”、“基金”、“资产净值”、“ID”)。head()返回数据帧。您可能对pandas用作web请求后端的urllib有问题。你能包括完整的错误回溯吗?是的。。。只需运行以下命令:
pd.read\u csv('https://seb.se/pow/fmk/2100/fonder_2019-10-17.TXT,encoding='iso-8859-1',header=None,sep=';',name=[“日期”、“基金”、“资产净值”、“ID”])
works