|
1478 | 1478 | {
|
1479 | 1479 | "data": {
|
1480 | 1480 | "text/plain": [
|
1481 |
| - "<pandas.io.parsers.readers.TextFileReader at 0x137d11220>" |
| 1481 | + "<pandas.io.parsers.readers.TextFileReader at 0x116412300>" |
1482 | 1482 | ]
|
1483 | 1483 | },
|
1484 | 1484 | "execution_count": 16,
|
|
1746 | 1746 | " print(line)"
|
1747 | 1747 | ]
|
1748 | 1748 | },
|
| 1749 | + { |
| 1750 | + "cell_type": "markdown", |
| 1751 | + "id": "0ed726c4-5e09-4676-bcf0-f78e9f7a10e0", |
| 1752 | + "metadata": {}, |
| 1753 | + "source": [ |
| 1754 | + "[Sniffer.has_header](https://docs.python.org/3/library/csv.html#csv.Sniffer.has_header) analyses your csv file and returns ``True`` if the first row appears to be a series of column headers.\n", |
| 1755 | + "\n", |
| 1756 | + "<div class=\"alert alert-block alert-info\">\n", |
| 1757 | + "\n", |
| 1758 | + "**Note:**\n", |
| 1759 | + "\n", |
| 1760 | + "This method is only a rough heuristic and can produce both false-positive and false-negative results.\n", |
| 1761 | + "</div>" |
| 1762 | + ] |
| 1763 | + }, |
| 1764 | + { |
| 1765 | + "cell_type": "markdown", |
| 1766 | + "id": "a19c05c1-e947-471b-8089-8e36e65b4268", |
| 1767 | + "metadata": {}, |
| 1768 | + "source": [ |
| 1769 | + "[Sniffer.sniff](https://docs.python.org/3/library/csv.html#csv.Sniffer.sniff) also analyses your csv file, but returns one of the following dialect subclasses." |
| 1770 | + ] |
| 1771 | + }, |
| 1772 | + { |
| 1773 | + "cell_type": "code", |
| 1774 | + "execution_count": 26, |
| 1775 | + "id": "263a8cb4-4ae1-46f0-963f-9d2df2de45ed", |
| 1776 | + "metadata": {}, |
| 1777 | + "outputs": [ |
| 1778 | + { |
| 1779 | + "name": "stdout", |
| 1780 | + "output_type": "stream", |
| 1781 | + "text": [ |
| 1782 | + "['', 'Title', 'Language', 'Authors', 'License', 'Publication date', 'doi']\n", |
| 1783 | + "['0', 'Python basics', 'en', 'Veit Schiele', 'BSD-3-Clause', '2021-10-28', '']\n", |
| 1784 | + "['1', 'Jupyter Tutorial', 'en', 'Veit Schiele', 'BSD-3-Clause', '2019-06-27', '']\n", |
| 1785 | + "['2', 'Jupyter Tutorial', 'de', 'Veit Schiele', 'BSD-3-Clause', '2020-10-26', '']\n", |
| 1786 | + "['3', 'PyViz Tutorial', 'en', 'Veit Schiele', 'BSD-3-Clause', '2020-04-13', '']\n" |
| 1787 | + ] |
| 1788 | + } |
| 1789 | + ], |
| 1790 | + "source": [ |
| 1791 | + "with open('out.csv') as f:\n", |
| 1792 | + " dialect = csv.Sniffer().sniff(f.read(1024))\n", |
| 1793 | + " f.seek(0)\n", |
| 1794 | + " reader = csv.reader(f, dialect)\n", |
| 1795 | + "\n", |
| 1796 | + " for line in reader:\n", |
| 1797 | + " print(line)" |
| 1798 | + ] |
| 1799 | + }, |
1749 | 1800 | {
|
1750 | 1801 | "cell_type": "markdown",
|
1751 | 1802 | "id": "3bc7ee20",
|
1752 | 1803 | "metadata": {},
|
1753 | 1804 | "source": [
|
1754 |
| - "### Dialekte\n", |
| 1805 | + "### Dialects\n", |
1755 | 1806 | "\n",
|
1756 |
| - "csv-Dateien gibt es in vielen verschiedenen Varianten. Das Python csv-Modul kommt bereits mit drei verschiedenen Dialekten:\n", |
| 1807 | + "csv files are available in many different variants. The Python csv module already comes with three different dialects:\n", |
1757 | 1808 | "\n",
|
1758 |
| - "Parameter | excel | excel-tab | unix\n", |
| 1809 | + "Parameters | [excel](https://docs.python.org/3/library/csv.html#csv.excel) | [excel-tab](https://docs.python.org/3/library/csv.html#csv.excel_tab) | [unix](https://docs.python.org/3/library/csv.html#csv.unix_dialect)\n", |
1759 | 1810 | ":--- | :--- | :--- | :---\n",
|
1760 | 1811 | "`delimiter` | `','` | `'\\\\t'` | `','`\n",
|
1761 | 1812 | "`quotechar` | `'\\\"'` | `'\\\"'` | ` '\\\"'`\n",
|
|
1780 | 1831 | },
|
1781 | 1832 | {
|
1782 | 1833 | "cell_type": "code",
|
1783 |
| - "execution_count": 26, |
| 1834 | + "execution_count": 27, |
1784 | 1835 | "id": "c6d73a1e",
|
1785 | 1836 | "metadata": {},
|
1786 | 1837 | "outputs": [],
|
|
1804 | 1855 | },
|
1805 | 1856 | {
|
1806 | 1857 | "cell_type": "code",
|
1807 |
| - "execution_count": 27, |
| 1858 | + "execution_count": 28, |
1808 | 1859 | "id": "85ac6d66",
|
1809 | 1860 | "metadata": {},
|
1810 | 1861 | "outputs": [
|
|
1837 | 1888 | },
|
1838 | 1889 | {
|
1839 | 1890 | "cell_type": "code",
|
1840 |
| - "execution_count": 28, |
| 1891 | + "execution_count": 29, |
1841 | 1892 | "id": "341af079",
|
1842 | 1893 | "metadata": {},
|
1843 | 1894 | "outputs": [
|
|
1856 | 1907 | " 'doi': ('', '', '', '')}"
|
1857 | 1908 | ]
|
1858 | 1909 | },
|
1859 |
| - "execution_count": 28, |
| 1910 | + "execution_count": 29, |
1860 | 1911 | "metadata": {},
|
1861 | 1912 | "output_type": "execute_result"
|
1862 | 1913 | }
|
|
1881 | 1932 | },
|
1882 | 1933 | {
|
1883 | 1934 | "cell_type": "code",
|
1884 |
| - "execution_count": 29, |
| 1935 | + "execution_count": 30, |
1885 | 1936 | "id": "69f3c21a",
|
1886 | 1937 | "metadata": {},
|
1887 | 1938 | "outputs": [],
|
|
1895 | 1946 | },
|
1896 | 1947 | {
|
1897 | 1948 | "cell_type": "code",
|
1898 |
| - "execution_count": 30, |
| 1949 | + "execution_count": 31, |
1899 | 1950 | "id": "ff5b4f67",
|
1900 | 1951 | "metadata": {},
|
1901 | 1952 | "outputs": [
|
|
1907 | 1958 | " '2,Jupyter Tutorial,en,Veit Schiele\\n']"
|
1908 | 1959 | ]
|
1909 | 1960 | },
|
1910 |
| - "execution_count": 30, |
| 1961 | + "execution_count": 31, |
1911 | 1962 | "metadata": {},
|
1912 | 1963 | "output_type": "execute_result"
|
1913 | 1964 | }
|
|
0 commit comments