Pandas Bad Lines Warning Capture

后端 未结 2 1237
情深已故
情深已故 2020-12-03 12:17

Is there any way in Pandas to capture the warning produced by setting error_bad_lines = False and warn_bad_lines = True? For instance the following script:



        
2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 12:43

    I can't help you with older than Python 3, but I've had very good success with the following:

    import pandas as pd
    from contextlib import redirect_stderr
    import io
    
    # Redirect stderr to something we can report on.
    f = io.StringIO()
    with redirect_stderr(f):
        df = pd.read_csv(
            new_file_name, header=None, error_bad_lines=False, warn_bad_lines=True, dtype=header_types
        )
    if f.getvalue():
        logger.warning("Had parsing errors: {}".format(f.getvalue()))
    

    I searched for this issue a number of times and kept being pointed to this questions. Hope it helps someone else, later on.

提交回复
热议问题