343
343
Alias for sep.
344
344
"""
345
345
346
- _fwf_widths = r"""
347
- colspecs : list of pairs (int, int) or 'infer'. optional
348
- A list of pairs (tuples) giving the extents of the fixed-width
349
- fields of each line as half-open intervals (i.e., [from, to[ ).
350
- String value 'infer' can be used to instruct the parser to try
351
- detecting the column specifications from the first 100 rows of
352
- the data which are not being skipped via skiprows (default='infer').
353
- widths : list of ints. optional
354
- A list of field widths which can be used instead of 'colspecs' if
355
- the intervals are contiguous.
356
- delimiter : str, default ``'\t' + ' '``
357
- Characters to consider as filler characters in the fixed-width file.
358
- Can be used to specify the filler character of the fields
359
- if it is not spaces (e.g., '~').
360
- **kwds : optional
361
- All the following keyword arguments can be passed.
362
- """
363
-
364
346
365
347
def _validate_integer (name , val , min_val = 0 ):
366
348
"""
@@ -726,13 +708,64 @@ def parser_f(filepath_or_buffer,
726
708
)(read_table )
727
709
728
710
729
- @Appender (_parser_params .format (
730
- func_name = 'read_fwf' ,
731
- summary = ('Read a table of fixed-width formatted lines '
732
- 'into DataFrame.' ),
733
- sep_doc = _fwf_widths ,
734
- engine_doc = '' ))
735
- def read_fwf (filepath_or_buffer , colspecs = 'infer' , widths = None , ** kwds ):
711
+ def read_fwf (filepath_or_buffer , delimiter = '\t ' + ' ' ,
712
+ colspecs = 'infer' , widths = None , ** kwds ):
713
+
714
+ r"""
715
+ Read a table of fixed-width formatted lines into DataFrame.
716
+
717
+ Also supports optionally iterating or breaking of the file
718
+ into chunks.
719
+
720
+ Additional help can be found in the `online docs for IO Tools
721
+ <http://pandas.pydata.org/pandas-docs/stable/io.html>`_.
722
+
723
+ Parameters
724
+ ----------
725
+ filepath_or_buffer : str, path object, or file-like object
726
+ Any valid string path is acceptable. The string could be a URL. Valid
727
+ URL schemes include http, ftp, s3, and file. For file URLs, a host is
728
+ expected. A local file could be: file://localhost/path/to/table.csv.
729
+
730
+ If you want to pass in a path object, pandas accepts either
731
+ ``pathlib.Path`` or ``py._path.local.LocalPath``.
732
+
733
+ By file-like object, we refer to objects with a ``read()`` method,
734
+ such as a file handler (e.g. via builtin ``open`` function)
735
+ or ``StringIO``.
736
+ delimiter : str, default ``'\t'+' '``
737
+ Characters to consider as filler characters in the fixed-width file.
738
+ Can be used to specify the filler character of the fields
739
+ if it is not spaces (e.g., '~').
740
+ colspecs : list of pairs (int, int) or 'infer'. optional
741
+ A list of pairs (tuples) giving the extents of the fixed-width
742
+ fields of each line as half-open intervals (i.e., [from, to[ ).
743
+ String value 'infer' can be used to instruct the parser to try
744
+ detecting the column specifications from the first 100 rows of
745
+ the data which are not being skipped via skiprows (default='infer').
746
+ widths : list of ints. optional
747
+ A list of field widths which can be used instead of 'colspecs' if
748
+ the intervals are contiguous.
749
+ **kwds : optional
750
+ Optional keyword arguments can be passed to ``TextFileReader``.
751
+
752
+ Returns
753
+ -------
754
+ DataFrame or TextParser
755
+ A comma-separated values (csv) file is returned as two-dimensional
756
+ data structure with labeled axes.
757
+
758
+ See Also
759
+ --------
760
+ to_csv : Write DataFrame to a comma-separated values (csv) file.
761
+ read_csv : Read a comma-separated values (csv) file into DataFrame.
762
+ read_fwf : Read a table of fixed-width formatted lines into DataFrame.
763
+
764
+ Examples
765
+ --------
766
+ >>> pd.read_fwf('data.csv') # doctest: +SKIP
767
+ """
768
+
736
769
# Check input arguments.
737
770
if colspecs is None and widths is None :
738
771
raise ValueError ("Must specify either colspecs or widths" )
@@ -747,6 +780,7 @@ def read_fwf(filepath_or_buffer, colspecs='infer', widths=None, **kwds):
747
780
colspecs .append ((col , col + w ))
748
781
col += w
749
782
783
+ kwds ['delimiter' ] = delimiter
750
784
kwds ['colspecs' ] = colspecs
751
785
kwds ['engine' ] = 'python-fwf'
752
786
return _read (filepath_or_buffer , kwds )
0 commit comments