|
244 | 244 | standard encodings
|
245 | 245 | <https://docs.python.org/3/library/codecs.html#standard-encodings>`_
|
246 | 246 | dialect : str or csv.Dialect instance, default None
|
247 |
| - If None defaults to Excel dialect. Ignored if sep longer than 1 char |
248 |
| - See csv.Dialect documentation for more details |
| 247 | + If provided, this parameter will override values (default or not) for the |
| 248 | + following parameters: `delimiter`, `doublequote`, `escapechar`, |
| 249 | + `skipinitialspace`, `quotechar`, and `quoting`. If it is necessary to |
| 250 | + override values, a ParserWarning will be issued. See csv.Dialect |
| 251 | + documentation for more details. |
249 | 252 | tupleize_cols : boolean, default False
|
250 | 253 | Leave a list of tuples on columns as is (default is to convert to
|
251 | 254 | a Multi Index on the columns)
|
@@ -698,12 +701,33 @@ def __init__(self, f, engine=None, **kwds):
|
698 | 701 | dialect = kwds['dialect']
|
699 | 702 | if dialect in csv.list_dialects():
|
700 | 703 | dialect = csv.get_dialect(dialect)
|
701 |
| - kwds['delimiter'] = dialect.delimiter |
702 |
| - kwds['doublequote'] = dialect.doublequote |
703 |
| - kwds['escapechar'] = dialect.escapechar |
704 |
| - kwds['skipinitialspace'] = dialect.skipinitialspace |
705 |
| - kwds['quotechar'] = dialect.quotechar |
706 |
| - kwds['quoting'] = dialect.quoting |
| 704 | + |
| 705 | + # Any valid dialect should have these attributes. |
| 706 | + # If any are missing, we will raise automatically. |
| 707 | + for param in ('delimiter', 'doublequote', 'escapechar', |
| 708 | + 'skipinitialspace', 'quotechar', 'quoting'): |
| 709 | + try: |
| 710 | + dialect_val = getattr(dialect, param) |
| 711 | + except AttributeError: |
| 712 | + raise ValueError("Invalid dialect '{dialect}' provided" |
| 713 | + .format(dialect=kwds['dialect'])) |
| 714 | + provided = kwds.get(param, _parser_defaults[param]) |
| 715 | + |
| 716 | + # Messages for conflicting values between the dialect instance |
| 717 | + # and the actual parameters provided. |
| 718 | + conflict_msgs = [] |
| 719 | + |
| 720 | + if dialect_val != provided: |
| 721 | + conflict_msgs.append(( |
| 722 | + "Conflicting values for '{param}': '{val}' was " |
| 723 | + "provided, but the dialect specifies '{diaval}'. " |
| 724 | + "Using the dialect-specified value.".format( |
| 725 | + param=param, val=provided, diaval=dialect_val))) |
| 726 | + |
| 727 | + if conflict_msgs: |
| 728 | + warnings.warn('\n\n'.join(conflict_msgs), ParserWarning, |
| 729 | + stacklevel=2) |
| 730 | + kwds[param] = dialect_val |
707 | 731 |
|
708 | 732 | if kwds.get('header', 'infer') == 'infer':
|
709 | 733 | kwds['header'] = 0 if kwds.get('names') is None else None
|
|
0 commit comments