当使用read_csv时,我得到sre_constants.error:在位置0不再重复

时间:2018-06-28 10:09:13

标签: python pandas

我正在使用熊猫。我已经下载了成人数据集并将其转换为csv文件,现在我正尝试使用read_csv函数读取csv文件,但出现以下错误:

 sre_constants.error: nothing to repeat at position 0

这是我的代码中发生错误的行-

    adult_df = pd.read_csv('E:/iris dataset/adultdata.csv',header=None, 
    delimiter='*,*', engine='python')
    print(adult_df.isnull().sum())

这里是追溯-

        C:\Users\Sharanya\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sklearn\cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
  "This module will be removed in 0.20.", DeprecationWarning)
Traceback (most recent call last):
  File "E:\iris dataset\naive_bayes_adult_dataset.py", line 9, in <module>
    adult_df = pd.read_csv('E:/iris dataset/adultdata.csv',header=None, delimiter='*,*', engine='python')
  File "C:\Users\Sharanya\AppData\Roaming\Python\Python36\site-packages\pandas\io\parsers.py", line 678, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\Sharanya\AppData\Roaming\Python\Python36\site-packages\pandas\io\parsers.py", line 440, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "C:\Users\Sharanya\AppData\Roaming\Python\Python36\site-packages\pandas\io\parsers.py", line 787, in __init__
    self._make_engine(self.engine)
  File "C:\Users\Sharanya\AppData\Roaming\Python\Python36\site-packages\pandas\io\parsers.py", line 1024, in _make_engine
    self._engine = klass(self.f, **self.options)
  File "C:\Users\Sharanya\AppData\Roaming\Python\Python36\site-packages\pandas\io\parsers.py", line 2089, in __init__
    self.columns, self.num_original_columns = self._infer_columns()
  File "C:\Users\Sharanya\AppData\Roaming\Python\Python36\site-packages\pandas\io\parsers.py", line 2455, in _infer_columns
    line = self._buffered_line()
  File "C:\Users\Sharanya\AppData\Roaming\Python\Python36\site-packages\pandas\io\parsers.py", line 2530, in _buffered_line
    return self._next_line()
  File "C:\Users\Sharanya\AppData\Roaming\Python\Python36\site-packages\pandas\io\parsers.py", line 2635, in _next_line
    orig_line = self._next_iter_line(row_num=self.pos + 1)
  File "C:\Users\Sharanya\AppData\Roaming\Python\Python36\site-packages\pandas\io\parsers.py", line 2695, in _next_iter_line
    return next(self.data)
  File "C:\Users\Sharanya\AppData\Roaming\Python\Python36\site-packages\pandas\io\parsers.py", line 2229, in _read
    pat = re.compile(sep)
  File "C:\Users\Sharanya\AppData\Local\Programs\Python\Python36-32\lib\re.py", line 233, in compile
    return _compile(pattern, flags)
  File "C:\Users\Sharanya\AppData\Local\Programs\Python\Python36-32\lib\re.py", line 301, in _compile
    p = sre_compile.compile(pattern, flags)
  File "C:\Users\Sharanya\AppData\Local\Programs\Python\Python36-32\lib\sre_compile.py", line 562, in compile
    p = sre_parse.parse(p, flags)
  File "C:\Users\Sharanya\AppData\Local\Programs\Python\Python36-32\lib\sre_parse.py", line 855, in parse
    p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
  File "C:\Users\Sharanya\AppData\Local\Programs\Python\Python36-32\lib\sre_parse.py", line 416, in _parse_sub
    not nested and not items))
  File "C:\Users\Sharanya\AppData\Local\Programs\Python\Python36-32\lib\sre_parse.py", line 616, in _parse
    source.tell() - here + len(this))
sre_constants.error: nothing to repeat at position 0

1 个答案:

答案 0 :(得分:1)

参数delimiter期望使用单个字符定界符,或者如果其超过一个字符,则将其视为RegEx。

星号(*)在RegEx中具有特殊含义-重复前一个字符。

对于您的情况delimiter='*,*',第一个星号没有重复的内容。

因此,请尝试以下操作:

delimiter='\*,\*'

如果您确实有*,*作为分隔符