pandas.read_fwf#

pandas.read_fwf(filepath_or_buffer, *, colspecs='infer', widths=None, infer_nrows=100, iterator=False, chunksize=None, **kwds)[source]#

Read a table of fixed-width formatted lines into DataFrame.

Also supports optionally iterating or breaking of the file into chunks.

Additional help can be found in the online docs for IO Tools.

Parameters:
filepath_or_bufferstr, path object, or file-like object

Any valid string path is acceptable. The string could be a URL. Valid URL schemes include http, ftp, s3, and file. For file URLs, a host is expected. A local file could be: file://localhost/path/to/table.csv.

Certain URL schemes may require additional packages. For example, S3 URLs require the s3fs library. See Optional dependencies for a full list.

If you want to pass in a path object, pandas accepts any os.PathLike.

By file-like object, we refer to objects with a read() method that accepts an optional size argument, such as a file handle (e.g. via builtin open function) or StringIO.

colspecslist of tuple (int, int) or ‘infer’. optional

A list of tuples giving the extents of the fixed-width fields of each line as half-open intervals (i.e., [from, to] ). String value ‘infer’ can be used to instruct the parser to try detecting the column specifications from the first 100 rows of the data which are not being skipped via skiprows (default=’infer’).

widthslist of int, optional

A list of field widths which can be used instead of ‘colspecs’ if the intervals are contiguous.

infer_nrowsint, default 100

The number of rows to consider when letting the parser determine the colspecs. Pass np.inf to infer column specifications from the entire file, which is useful when columns vary in width and a value wider than anything in the first infer_nrows rows would otherwise be truncated.

iteratorbool, default False

Return TextFileReader object for iteration or getting chunks with get_chunk().

chunksizeint, optional

Number of lines to read from the file per chunk.

**kwdsoptional

Optional keyword arguments can be passed to TextFileReader.

Returns:
DataFrame or TextFileReader

A comma-separated values (csv) file is returned as two-dimensional data structure with labeled axes.

See also

DataFrame.to_csv

Write DataFrame to a comma-separated values (csv) file.

read_csv

Read a comma-separated values (csv) file into DataFrame.

Examples

>>> pd.read_fwf("data.csv")