【问题标题】:Can't import csv file on to collab?无法将 csv 文件导入到协作中?
【发布时间】:2020-05-27 20:22:12
【问题描述】:

我对 python 很陌生,一直在尝试将 csv 文件放到谷歌协作笔记本上。我不确定我做错了什么。已经尝试过使用“/”,但是它们也无法正常工作。

import pandas as pd
testData = pd.read_csv(r'C:\Users\User\Documents\ml\training.csv')

我收到多个错误:


FileNotFoundError                         Traceback (most recent call last)
<ipython-input-37-d77976d861ee> in <module>()
      1 import pandas as pd
----> 2 testData = pd.read_csv(r'C:\Users\User\Documents\ml\training.csv')
      3 
      4 

4 frames
/usr/local/lib/python3.6/dist-packages/pandas/io/parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, dialect, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision)
    674         )
    675 
--> 676         return _read(filepath_or_buffer, kwds)
    677 
    678     parser_f.__name__ = name

/usr/local/lib/python3.6/dist-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
    446 
    447     # Create the parser.
--> 448     parser = TextFileReader(fp_or_buf, **kwds)
    449 
    450     if chunksize or iterator:

/usr/local/lib/python3.6/dist-packages/pandas/io/parsers.py in __init__(self, f, engine, **kwds)
    878             self.options["has_index_names"] = kwds["has_index_names"]
    879 
--> 880         self._make_engine(self.engine)
    881 
    882     def close(self):

/usr/local/lib/python3.6/dist-packages/pandas/io/parsers.py in _make_engine(self, engine)
   1112     def _make_engine(self, engine="c"):
   1113         if engine == "c":
-> 1114             self._engine = CParserWrapper(self.f, **self.options)
   1115         else:
   1116             if engine == "python":

/usr/local/lib/python3.6/dist-packages/pandas/io/parsers.py in __init__(self, src, **kwds)
   1889         kwds["usecols"] = self.usecols
   1890 
-> 1891         self._reader = parsers.TextReader(src, **kwds)
   1892         self.unnamed_cols = self._reader.unnamed_cols
   1893 

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.__cinit__()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._setup_parser_source()

FileNotFoundError: [Errno 2] File C:\Users\User\Documents\ml\training.csv does not exist: 'C:\\Users\\User\\Documents\\ml\\training.csv'

【问题讨论】:

    标签: python csv compiler-errors path file-not-found


    【解决方案1】:

    Colab 无法从您的本地设备读取文件,它在云上的服务器上运行,并且无权访问您的本地文件系统。您需要上传您的文件。有很多方法可以做到这一点,请查找最适合您需求的方法。

    提示您上传文件然后您可以将其读入笔记本的示例。

    import io
    from google.colab import files
    uploaded = files.upload()
    
    
    df = pd.read_csv(io.BytesIO(uploaded['filename.csv']))
    

    【讨论】:

    猜你喜欢
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多