• pandas.errors.ParserError: Error tokenizing data. C error: Expected 1 fields in line 4, saw 2


    pandas.errors.ParserError: Error tokenizing data. C error: Expected 1 fields in line 4, saw 2
    "D:Program FilesPython36-32python.exe" D:/PyCharm_Project/bishe/process/read_csv.py
    Traceback (most recent call last):
      File "D:/PyCharm_Project/bishe/process/read_csv.py", line 11, in <module>
        df = pd.read_csv(csv_path) #报错
      File "D:Program FilesPython36-32libsite-packagespandasioparsers.py", line 676, in parser_f
        return _read(filepath_or_buffer, kwds)
      File "D:Program FilesPython36-32libsite-packagespandasioparsers.py", line 454, in _read
        data = parser.read(nrows)
      File "D:Program FilesPython36-32libsite-packagespandasioparsers.py", line 1133, in read
        ret = self._engine.read(nrows)
      File "D:Program FilesPython36-32libsite-packagespandasioparsers.py", line 2037, in read
        data = self._reader.read(nrows)
      File "pandas\_libsparsers.pyx", line 860, in pandas._libs.parsers.TextReader.read
      File "pandas\_libsparsers.pyx", line 875, in pandas._libs.parsers.TextReader._read_low_memory
      File "pandas\_libsparsers.pyx", line 929, in pandas._libs.parsers.TextReader._read_rows
      File "pandas\_libsparsers.pyx", line 916, in pandas._libs.parsers.TextReader._tokenize_rows
      File "pandas\_libsparsers.pyx", line 2071, in pandas._libs.parsers.raise_parser_error
    pandas.errors.ParserError: Error tokenizing data. C error: Expected 1 fields in line 4, saw 2
    

    查了下资料,应该是我强行转换格式(xlsx->csv)所引起的字符编码问题
    这里稍微总结一下由字符编码问题引起的错误该如何解决办法呢,如下:

    • 文件另存为csv
    • 如果不是像我那样强转所导致的,就增加分隔符参数
      df = pd.read_csv(csv_path)
      df = pd.read_csv(csv_path, encoding='utf-8',sep = '	')
    

    或者增添这个参数

      df = pd.read_csv(csv_path, error_bad_lines=False) #报错
    

    再或者增添这个参数

      df = pd.read_csv(csv_path, engine="python") #报错
    

    参考文章
    https://www.jianshu.com/p/be233bdb4dbf
    https://blog.csdn.net/shuiyixin/article/details/88930359

  • 相关阅读:
    leetcode 3sum
    leetcode majority elements
    php调取linux的压缩命令进行压缩
    mysql 分区、分表、分库分表。
    如何在数据库中使用索引
    PDO 的基本操作
    JavaScript中常见的字符串操作函数及用法汇总
    php-fpm
    phpmyadmin导入导出数据库文件最大限制的解决方法
    php 获取今日、昨日、上周、本月的起始时间戳和结束时间戳。
  • 原文地址:https://www.cnblogs.com/z-712/p/12703217.html
Copyright © 2020-2023  润新知