簡體   English   中英

我的 python 無法從 github 讀取數據

[英]My python not able to read data from github

這是我的代碼:-

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
  dataset =  pd.read_csv('https://raw.githubusercontent.com/mk- gurucharan/Classification/master /SocialNetworkAds.csv')X = dataset.iloc[:, [0,  1]].values
  y = dataset.iloc[:, 2].valuesdataset.head(5)

它顯示的錯誤是:-

 /Users/apple/PycharmProjects/pythonProject4 /venv/bin/python /Users/apple/PycharmProjects /pythonProject4/main.py 
   File "/Users/apple/PycharmProjects /pythonProject4/main.py", line 4
   dataset =  pd.read_csv('https://raw.githubusercontent.com/mk- gurucharan/Classification/master /SocialNetworkAds.csv')X = dataset.iloc[:, [0,  1]].values
                                                                                                                   ^
 SyntaxError: invalid syntax

 Process finished with exit code 1

為什么無法從 github 讀取數據。

編輯:這是我閱讀評論后的新代碼:-

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset =  pd.read_csv('https://raw.githubusercontent.com/mk- gurucharan/Classification/master/SocialNetworkAds.csv')
X = dataset.iloc[:, [0,  1]].values
y = dataset.iloc[:, 2].values
print(dataset) 

它顯示的錯誤是:-

  /Users/apple/PycharmProjects/pythonProject4/venv/bin/python   /Users/apple/PycharmProjects/pythonProject4/main.py 
   Traceback (most recent call last):
   File "/Library/Frameworks/Python.framework/Versions/3.10/lib   /python3.10/urllib/request.py", line 1348, in do_open
   h.request(req.get_method(), req.selector, req.data, headers,
   File "/Library/Frameworks/Python.framework/Versions/3.10/lib   /python3.10/http/client.py", line 1282, in request
   self._send_request(method, url, body, headers, encode_chunked)
   File "/Library/Frameworks/Python.framework/Versions/3.10/lib    /python3.10/http/client.py", line 1328, in _send_request
   self.endheaders(body, encode_chunked=encode_chunked)
   File "/Library/Frameworks/Python.framework/Versions/3.10/lib   /python3.10/http/client.py", line 1277, in endheaders
   self._send_output(message_body, encode_chunked=encode_chunked)
   File "/Library/Frameworks/Python.framework/Versions/3.10/lib   /python3.10/http/client.py", line 1037, in _send_output
   self.send(msg)
   File "/Library/Frameworks/Python.framework/Versions/3.10/lib  /python3.10/http/client.py", line 975, in send
   self.connect()
   File "/Library/Frameworks/Python.framework/Versions/3.10/lib  /python3.10/http/client.py", line 1447, in connect
   super().connect()
   File "/Library/Frameworks/Python.framework/Versions/3.10/lib  /python3.10/http/client.py", line 941, in connect
   self.sock = self._create_connection(
   File "/Library/Frameworks/Python.framework/Versions/3.10/lib  /python3.10/socket.py", line 845, in create_connection
   raise err
   File "/Library/Frameworks/Python.framework/Versions/3.10/lib  /python3.10/socket.py", line 833, in create_connection
   sock.connect(sa)
   TimeoutError: [Errno 60] Operation timed out

   During handling of the above exception, another exception occurred:

   Traceback (most recent call last):
    File "/Users/apple/PycharmProjects/pythonProject4/main.py", line 4, in <module>
       dataset =  pd.read_csv('https://raw.githubusercontent.com   /mk-gurucharan/Classification/master/SocialNetworkAds.csv')
     File "/Users/apple/PycharmProjects/pythonProject4/venv/lib /python3.10/site-packages/pandas/util/_decorators.py", line 211, in       wrapper
    return func(*args, **kwargs)
    File "/Users/apple/PycharmProjects/pythonProject4/venv/lib /python3.10/site-packages/pandas/util/_decorators.py", line 331, in        wrapper
    return func(*args, **kwargs)
   File "/Users/apple/PycharmProjects/pythonProject4/venv/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 950, in   read_csv
   return _read(filepath_or_buffer, kwds)
   File "/Users/apple/PycharmProjects/pythonProject4/venv/lib  /python3.10/site-packages/pandas/io/parsers/readers.py", line 605, in    _read
   parser = TextFileReader(filepath_or_buffer, **kwds)
   File "/Users/apple/PycharmProjects/pythonProject4/venv/lib  /python3.10/site-packages/pandas/io/parsers/readers.py", line 1442, in   __init__
    self._engine = self._make_engine(f, self.engine)
     File "/Users/apple/PycharmProjects/pythonProject4/venv/lib   /python3.10/site-packages/pandas/io/parsers/readers.py", line 1735, in     _make_engine
     self.handles = get_handle(
     File "/Users/apple/PycharmProjects/pythonProject4/venv/lib/python3.10/site-packages/pandas/io/common.py", line 713, in get_handle
     ioargs = _get_filepath_or_buffer(
       File "/Users/apple/PycharmProjects/pythonProject4/venv/lib/python3.10/site-packages/pandas/io/common.py", line 363, in   _get_filepath_or_buffer
     with urlopen(req_info) as req:
     File "/Users/apple/PycharmProjects/pythonProject4/venv/lib /python3.10/site-packages/pandas/io/common.py", line 265, in urlopen
     return urllib.request.urlopen(*args, **kwargs)
      File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/request.py", line 216, in urlopen
     return opener.open(url, data, timeout)
       File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/request.py", line 519, in open
      response = self._open(req, data)
       File "/Library/Frameworks/Python.framework/Versions/3.10/lib /python3.10/urllib/request.py", line 536, in _open
       result = self._call_chain(self.handle_open, protocol, protocol +
      File "/Library/Frameworks/Python.framework/Versions/3.10/lib    /python3.10/urllib/request.py", line 496, in _call_chain
      result = func(*args)
      File "/Library/Frameworks/Python.framework/Versions/3.10/lib /python3.10/urllib/request.py", line 1391, in https_open
      return self.do_open(http.client.HTTPSConnection, req,
      File "/Library/Frameworks/Python.framework/Versions/3.10/lib /python3.10/urllib/request.py", line 1351, in do_open
     raise URLError(err)
      urllib.error.URLError: <urlopen error [Errno 60] Operation timed out>

        Process finished with exit code 1

如何應對? 我猜它仍然沒有正確讀取數據。 我應該怎么辦。 如何消除這些錯誤。 根據我的說法,一切都是正確的。

從 url 中刪除空格?

#                                                     HERE --v                             --v
dataset =  pd.read_csv('https://raw.githubusercontent.com/mk-gurucharan/Classification/master/SocialNetworkAds.csv')
X = dataset.iloc[:, [0,  1]].values
y = dataset.iloc[:, 2].values
print(dataset)

# Output
     Age  EstimatedSalary  Purchased
0     19            19000          0
1     35            20000          0
2     26            43000          0
3     27            57000          0
4     19            76000          0
..   ...              ...        ...
395   46            41000          1
396   51            23000          1
397   50            20000          1
398   36            33000          0
399   49            36000          1

[400 rows x 3 columns]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM