|
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 for the following |
| 248 | + parameters: `delimiter`, `doublequote`, `escapechar`, `skipinitialspace`, |
| 249 | + `quotechar`, and `quoting`. See csv.Dialect documentation for more details. |
249 | 250 | tupleize_cols : boolean, default False |
250 | 251 | Leave a list of tuples on columns as is (default is to convert to |
251 | 252 | a Multi Index on the columns) |
@@ -692,12 +693,33 @@ def __init__(self, f, engine=None, **kwds): |
692 | 693 | dialect = kwds['dialect'] |
693 | 694 | if dialect in csv.list_dialects(): |
694 | 695 | dialect = csv.get_dialect(dialect) |
695 | | - kwds['delimiter'] = dialect.delimiter |
696 | | - kwds['doublequote'] = dialect.doublequote |
697 | | - kwds['escapechar'] = dialect.escapechar |
698 | | - kwds['skipinitialspace'] = dialect.skipinitialspace |
699 | | - kwds['quotechar'] = dialect.quotechar |
700 | | - kwds['quoting'] = dialect.quoting |
| 696 | + |
| 697 | + # Any valid dialect should have these attributes. |
| 698 | + # If any are missing, we will raise automatically. |
| 699 | + for param in ('delimiter', 'doublequote', 'escapechar', |
| 700 | + 'skipinitialspace', 'quotechar', 'quoting'): |
| 701 | + try: |
| 702 | + dialect_val = getattr(dialect, param) |
| 703 | + except AttributeError: |
| 704 | + raise ValueError("Invalid dialect '{dialect}' provided" |
| 705 | + .format(dialect=kwds['dialect'])) |
| 706 | + provided = kwds.get(param, _parser_defaults[param]) |
| 707 | + |
| 708 | + # Messages for conflicting values between the dialect instance |
| 709 | + # and the actual parameters provided. |
| 710 | + conflict_msgs = [] |
| 711 | + |
| 712 | + if dialect_val != provided: |
| 713 | + conflict_msgs.append(( |
| 714 | + "Conflicting values for '{param}': '{val}' was " |
| 715 | + "provided, but the dialect specifies '{diaval}'. " |
| 716 | + "Using the dialect-specified value.".format( |
| 717 | + param=param, val=provided, diaval=dialect_val))) |
| 718 | + |
| 719 | + if conflict_msgs: |
| 720 | + warnings.warn('\n\n'.join(conflict_msgs), UserWarning, |
| 721 | + stacklevel=2) |
| 722 | + kwds[param] = dialect_val |
701 | 723 |
|
702 | 724 | if kwds.get('header', 'infer') == 'infer': |
703 | 725 | kwds['header'] = 0 if kwds.get('names') is None else None |
|
0 commit comments