/usr/share/doc/python-dbfread-doc/html/field_types.html is in python-dbfread-doc 2.0.7-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Field Types — dbfread 2.0.7 documentation</title>
<link rel="stylesheet" href="_static/classic.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '2.0.7',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="API Changes" href="api_changes.html" />
<link rel="prev" title="DBF Objects" href="dbf_objects.html" />
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="api_changes.html" title="API Changes"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="dbf_objects.html" title="DBF Objects"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">dbfread 2.0.7 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="field-types">
<h1>Field Types<a class="headerlink" href="#field-types" title="Permalink to this headline">¶</a></h1>
<div class="section" id="supported-field-types">
<h2>Supported Field Types<a class="headerlink" href="#supported-field-types" title="Permalink to this headline">¶</a></h2>
<table border="1" class="docutils">
<colgroup>
<col width="3%" />
<col width="18%" />
<col width="79%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">:</th>
<th class="head">Field type</th>
<th class="head">Converted to</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>+</td>
<td>autoincrement</td>
<td>int</td>
</tr>
<tr class="row-odd"><td>@</td>
<td>time</td>
<td>datetime.datetime</td>
</tr>
<tr class="row-even"><td>0</td>
<td>flags</td>
<td>byte string (int before 2.0)</td>
</tr>
<tr class="row-odd"><td>B</td>
<td>double</td>
<td>float (Visual FoxPro)</td>
</tr>
<tr class="row-even"><td>B</td>
<td>binary memo</td>
<td>byte string (other versions)</td>
</tr>
<tr class="row-odd"><td>C</td>
<td>text</td>
<td>unicode string</td>
</tr>
<tr class="row-even"><td>D</td>
<td>date</td>
<td>datetime.date or None</td>
</tr>
<tr class="row-odd"><td>F</td>
<td>float</td>
<td>float</td>
</tr>
<tr class="row-even"><td>G</td>
<td>OLE object</td>
<td>byte string</td>
</tr>
<tr class="row-odd"><td>I</td>
<td>integer</td>
<td>int</td>
</tr>
<tr class="row-even"><td>L</td>
<td>logical</td>
<td>True, False or None</td>
</tr>
<tr class="row-odd"><td>M</td>
<td>memo</td>
<td>unicode string (memo), byte string (picture or object)
or None</td>
</tr>
<tr class="row-even"><td>N</td>
<td>numeric</td>
<td>int, float or None</td>
</tr>
<tr class="row-odd"><td>O</td>
<td>double</td>
<td>float (floats are doubles in Python)</td>
</tr>
<tr class="row-even"><td>P</td>
<td>picture</td>
<td>byte string</td>
</tr>
<tr class="row-odd"><td>T</td>
<td>time</td>
<td>datetime.datetime</td>
</tr>
<tr class="row-even"><td>V</td>
<td>varchar</td>
<td>unicode string</td>
</tr>
<tr class="row-odd"><td>Y</td>
<td>currency</td>
<td>decimal.Decimal</td>
</tr>
</tbody>
</table>
<p>Text values (‘C’) can be up to 65535 bytes long. DBF was originally
limited to 255 bytes but some vendors have reused the
<code class="docutils literal"><span class="pre">decimal_count</span></code> field to get another byte for field length.</p>
<p>The ‘B’ field type is used to store double precision (64 bit) floats
in Visual FoxPro databases and binary memos in other
versions. <code class="docutils literal"><span class="pre">dbfread</span></code> will look at the database version to parse and
return the correct data type.</p>
<p>The ‘0’ field type is used for ‘_NullFlags’ in Visual FoxPro. It was
mistakenly though to always be one byte long and was interpreted as an
integer. From 2.0.1 on it is returned as a byte string.</p>
<p>The ‘V’ field is an alternative character field used by Visual
FoxPro. The binary version of this field is not yet supported. (See
<a class="reference external" href="https://msdn.microsoft.com/en-us/library/st4a0s68%28VS.80%29.aspx">https://msdn.microsoft.com/en-us/library/st4a0s68%28VS.80%29.aspx</a> for
more.)</p>
</div>
<div class="section" id="adding-custom-field-types">
<h2>Adding Custom Field Types<a class="headerlink" href="#adding-custom-field-types" title="Permalink to this headline">¶</a></h2>
<p>You can add new field types by subclassing
<code class="xref py py-class docutils literal"><span class="pre">FieldParser</span></code>. For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="sd">"""</span>
<span class="sd">Add custom field parsing by subclassing FieldParser.</span>
<span class="sd">"""</span>
<span class="kn">from</span> <span class="nn">dbfread</span> <span class="k">import</span> <span class="n">DBF</span><span class="p">,</span> <span class="n">FieldParser</span>
<span class="k">class</span> <span class="nc">CustomFieldParser</span><span class="p">(</span><span class="n">FieldParser</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">parseC</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
<span class="c1"># Return strings reversed.</span>
<span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="sa">b</span><span class="s1">' 0'</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">()[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="k">for</span> <span class="n">record</span> <span class="ow">in</span> <span class="n">DBF</span><span class="p">(</span><span class="s1">'files/people.dbf'</span><span class="p">,</span> <span class="n">parserclass</span><span class="o">=</span><span class="n">CustomFieldParser</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="n">record</span><span class="p">[</span><span class="s1">'NAME'</span><span class="p">])</span>
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">FieldParser</span></code> object has the following attributes:</p>
<dl class="docutils">
<dt>self.table</dt>
<dd>A reference to the <code class="docutils literal"><span class="pre">DBF</span></code> objects. This can be used to get the headers
to find <code class="docutils literal"><span class="pre">dbversion</span></code> and other things.</dd>
<dt>self.encoding</dt>
<dd>The character encoding. (A a shortcut for <code class="docutils literal"><span class="pre">self.table.encoding</span></code> to
speed things up a bit.)</dd>
<dt>self.char_decode_errors</dt>
<dd>Error handling scheme to use while decoding. (A shortcut for <code class="docutils literal"><span class="pre">self.table.char_decode_errors</span></code>.)</dd>
<dt>self.dbversion</dt>
<dd>The database version as an integer. (A shortcut for
<code class="docutils literal"><span class="pre">self.table.header.dbversion</span></code>.)</dd>
<dt>self.get_memo(index)</dt>
<dd><p class="first">Returns a memo from the memo file using the index stored in the field data.</p>
<p>This returns a byte string (<code class="docutils literal"><span class="pre">bytes</span></code>) which you can then
decode.</p>
<p>For Visual FoxPro (<code class="docutils literal"><span class="pre">.FPT</span></code>) files it will return <code class="docutils literal"><span class="pre">TextMemo</span></code>,
<code class="docutils literal"><span class="pre">PictureMemo</span></code> and <code class="docutils literal"><span class="pre">ObjectMemo</span></code> objects depending on the type of
memo. These are all subclasses of <code class="docutils literal"><span class="pre">bytes</span></code> so the type is only used
to annotate the memo type without breaking code elsewhere. The full
class tree:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="nb">bytes</span>
<span class="n">VFPMemo</span>
<span class="n">TextMemo</span>
<span class="n">BinaryMemo</span>
<span class="n">PictureMemo</span>
<span class="n">ObjectMemo</span>
</pre></div>
</div>
<p class="last">These are all found in <code class="docutils literal"><span class="pre">dbfread.memo</span></code>.</p>
</dd>
</dl>
<p>self.decode_text(text)</p>
<blockquote>
<div>This will decode the text using the correct encoding and the user
supplied <code class="docutils literal"><span class="pre">char_decode_errors</span></code> option.</div></blockquote>
</div>
<div class="section" id="special-characters-in-field-type-names">
<h2>Special Characters in Field Type Names<a class="headerlink" href="#special-characters-in-field-type-names" title="Permalink to this headline">¶</a></h2>
<p>For a field type like ‘+’ (autoincrement) the method would be named
<code class="docutils literal"><span class="pre">parse+()</span></code>. Since this is not allowed in Python you can instead use
its ASCII value in hexadecimal. For example, the ‘+’ parser is called
<code class="docutils literal"><span class="pre">parse3F()</span></code>.</p>
<p>You can name your method with:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="s1">'parse'</span> <span class="o">+</span> <span class="nb">format</span><span class="p">(</span><span class="nb">ord</span><span class="p">(</span><span class="s1">'?'</span><span class="p">),</span> <span class="s1">'x'</span><span class="p">)</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>
<span class="go">'parse3F'</span>
</pre></div>
</div>
<p>Just replace <code class="docutils literal"><span class="pre">'?'</span></code> with your field type.</p>
</div>
<div class="section" id="invalidvalue">
<h2>InvalidValue<a class="headerlink" href="#invalidvalue" title="Permalink to this headline">¶</a></h2>
<p>The field parser will normally raise <code class="docutils literal"><span class="pre">ValueError</span></code> when invalid
values are encountered. If instead you want them returned as raw data
you can do this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="sd">"""</span>
<span class="sd">A field parser that returns invalid values as InvalidValue objects</span>
<span class="sd">instead of raising ValueError.</span>
<span class="sd">"""</span>
<span class="kn">from</span> <span class="nn">dbfread</span> <span class="k">import</span> <span class="n">DBF</span><span class="p">,</span> <span class="n">FieldParser</span><span class="p">,</span> <span class="n">InvalidValue</span>
<span class="k">class</span> <span class="nc">MyFieldParser</span><span class="p">(</span><span class="n">FieldParser</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">parse</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
<span class="k">try</span><span class="p">:</span>
<span class="k">return</span> <span class="n">FieldParser</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
<span class="k">return</span> <span class="n">InvalidValue</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">table</span> <span class="o">=</span> <span class="n">DBF</span><span class="p">(</span><span class="s1">'files/invalid_value.dbf'</span><span class="p">,</span> <span class="n">parserclass</span><span class="o">=</span><span class="n">MyFieldParser</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">record</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">table</span><span class="p">):</span>
<span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">record</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">InvalidValue</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'records[</span><span class="si">{}</span><span class="s1">][</span><span class="si">{!r}</span><span class="s1">] == </span><span class="si">{!r}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span>
</pre></div>
</div>
<p><code class="docutils literal"><span class="pre">InvalidValue</span></code> is a subclass of <code class="docutils literal"><span class="pre">bytes</span></code>, and allows you to tell
invalid data apart from valid data that happens to be byte
strings. You can test for this with:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">InvalidData</span><span class="p">)</span>
</pre></div>
</div>
<p>You can also tell from the <code class="docutils literal"><span class="pre">repr()</span></code> string:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">value</span>
<span class="go">InvalidData(b'not a number')</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Field Types</a><ul>
<li><a class="reference internal" href="#supported-field-types">Supported Field Types</a></li>
<li><a class="reference internal" href="#adding-custom-field-types">Adding Custom Field Types</a></li>
<li><a class="reference internal" href="#special-characters-in-field-type-names">Special Characters in Field Type Names</a></li>
<li><a class="reference internal" href="#invalidvalue">InvalidValue</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="dbf_objects.html"
title="previous chapter">DBF Objects</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="api_changes.html"
title="next chapter">API Changes</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/field_types.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="api_changes.html" title="API Changes"
>next</a> |</li>
<li class="right" >
<a href="dbf_objects.html" title="DBF Objects"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">dbfread 2.0.7 documentation</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright Ole Martin Bjørndalen.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.3.
</div>
</body>
</html>
|