# 数据读取和输出 ## 读取格式 Function Description read_csv Load delimited data from a file, URL, or file-like object; use comma as default delimiter read_table Load delimited data from a file, URL, or file-like object; use tab (‘\t’) as default delimiter read_fwf Read data in fixed-width column format (i.e., no delimiters) read_clipboard Version of read_table that reads data from the clipboard; useful for converting tables from web pages read_excel Read tabular data from an Excel XLS or XLSX file read_hdf Read HDF5 files written by pandas read_html Read all tables found in the given HTML document read_json Read data from a JSON (JavaScript Object Notation) string representation read_msgpack Read pandas data encoded using the MessagePack binary format read_pickle Read an arbitrary object stored in Python pickle format read_sas Read a SAS dataset stored in one of the SAS system’s custom storage formats read_sql Read the results of a SQL query (using SQLAlchemy) as a pandas DataFrame read_stata Read a dataset from Stata file format read_feather Read the Feather binary file format
import pandas as pd
df = pd.read_csv('/Users/meininghang/Downloads/pydata-book-2nd-edition/examples/ex1.csv')
converters Dict containing column number of name mapping to functions (e.g., {‘foo’: f} would apply the function f to all values in the ‘foo’ column). dayfirst When parsing potentially ambiguous dates, treat as international format (e.g., 7/6/2012 -> June 7, 2012); False by default. date_parser Function to use to parse dates. nrows Number of rows to read from beginning of file. iterator Return a TextParser object for reading file piecemeal. chunksize For iteration, size of file chunks. skip_footer Number of lines to ignore at end of file. verbose Print various parser output information, like the number of missing values placed in non-numeric columns. encoding Text encoding for Unicode (e.g., ‘utf-8’ for UTF-8 encoded text). squeeze If the parsed data only contains one column, return a Series. thousands Separator for thousands (e.g., ‘,’ or ‘.’).Argument Description path String indicating filesystem location, URL, or file-like object sep or delimiter Character sequence or regular expression to use to split fields in each row header Row number to use as column names; defaults to 0 (first row), but should be None if there is no header row index_col Column numbers or names to use as the row index in the result; can be a single name/number or a list of them for a hierarchical index names List of column names for result, combine with header=None skiprows Number of rows at beginning of file to ignore or list of row numbers (starting from 0) to skip. na_values Sequence of values to replace with NA. comment Character(s) to split comments off the end of lines. parse_dates Attempt to parse data to datetime; False by default. If True, will attempt to parse all columns. Otherwise can specify a list of column numbers or name to parse. If element of list is tuple or list, will combine multiple columns together and parse to date (e.g., if date/time split across two columns). keep_date_col If joining columns to parse date, keep the joined columns; False by default.
### 部分
pd.options.display.max_rows = 10
result = pd.read_csv('/Users/meininghang/Downloads/pydata-book-2nd-edition/examples/ex6.csv')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-38-be4cd5a73166> in <module>()
5 quotechar = ' " '
6 quoting = csv.QUOTE_MINIMAL
----> 7 reader = csv.reader(f, dialect=my_dialect)
TypeError: argument 1 must be an iterator
Argument Description delimiter One-character string to separate fields; defaults to ‘,’. lineterminator Line terminator for writing; defaults to ‘\r\n’. Reader ignores this and recognizes cross-platform line terminators. quotechar Quote character for fields with special characters (like a delimiter); default is ‘”’. quoting Quoting convention. Options include csv.QUOTE_ALL (quote all fields), csv.QUOTE_MINIMAL (only fields with special characters like the delimiter), csv.QUOTE_NONNUMERIC, and csv.QUOTE_NONE (no quoting). See Python’s documentation for full details. Defaults to QUOTE_MINIMAL. skipinitialspace Ignore whitespace after each delimiter; default is False. doublequote How to handle quoting character inside a field; if True, it is doubled (see online documentation for full detail and behavior). escapechar String to escape the delimiter if quoting is set to csv.QUOTE_NONE; disabled by default.