⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 140.html

📁 Python Ebook Python&XML
💻 HTML
字号:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Developer's Handbook -&gt; Flat Databases</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="8.html" class="navtitle">Web Development</a> &gt; <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> &gt; <a href="137.html" class="navtitle">8. Working with Databases</a> &gt; <span class="nonavtitle">Flat Databases</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="139.html" title="Working with Databases"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=140" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="140.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="141.html" title="DBM (Database Managers) Databases"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A38%3A39+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162147044209056142149179093</font><a href="read8.asp?bookname=0672319942&snode=140&now=5%2F31%2F2002+4%3A38%3A39+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
				<h3>
			
			Flat Databases</h3>
				<p>The simplest way to store any kind of information in Python is using flat files. You just need to use the <tT CLAss="monofont">open</tt> function that we already studied in <A HREf="22.html">Chapter 2, "Language Review."
					</a>  Two options are available: You can either store the information as simple text or as binary data.</p>

				
					<h4>
				
				Text Data</H4>
					<P>The next example is a straightforward case of using flat files to store and to retrieve information. First we try to read from the file. If the file doesn't exist, it is created, and the information provided by the user is saved on it.</P>

					<Pre>
						
filename = "myflatfile.txt"
try:
    file = open(filename, "r")
    data = file.read()
    file.close()
    print data
except IOError:
    data = raw_input("Enter data to save:")
    file = open(filename,"w")
    file.write(data)
    file.close()

					</pre>

				
				
					<h4>
				
				
				
				Binary Data桾he <tt class="monofont">struct</tt> Module</h4>
					<p>The <tt cLasS="monofont">struct</tt> module is largely used to manipulate code of platform-independent binary files. It is a good choice for handling small files. For large files, you should consider using the <a Name="idx1073744834"></a>
						<A namE="idx1073744835"></A>
						<TT clasS="monofont">array</TT> module.</P>

					<p>Binary data files are much less likely to be platform independent. Also, it is easier to extend a text file format without breaking compatibility.</p>

					<p>The <tT CLAss="monofont">struct</tt> module works by converting data between Python and binary data structures, which normally interact using functions written in C.</P>

					<P>This module implements only three functions: <TT class="monofont">pack, unpack,</tt> and <tt class="monofont">calcsize.</tt>
					</p>

					<ul>
<lI>
							<p>
								
									<tT claSs="monofont">pack

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -