Skip to main content

2012 | OriginalPaper | Buchkapitel

6. Files, Strings, and Dictionaries

verfasst von : Hans Petter Langtangen

Erschienen in: A Primer on Scientific Programming with Python

Verlag: Springer Berlin Heidelberg

Aktivieren Sie unsere intelligente Suche, um passende Fachinhalte oder Patente zu finden.

search-config
loading …

Abstract

Files are used for permanent storage of information on a computer. This chapter tells you how Python programs can access information in files and also how to create new files. We show how file information can be mapped on to data in a program, and for this purpose dictionaries are particularly convenient. Interpreting and extracting data from files often imply sophisticated operations on text. We demonstrate how this is done by using the rich functionality of Python strings. There are also examples showing how to read World Wide Web pages as files and extract information from them.

Sie haben noch keine Lizenz? Dann Informieren Sie sich jetzt über unsere Produkte:

Springer Professional "Wirtschaft+Technik"

Online-Abonnement

Mit Springer Professional "Wirtschaft+Technik" erhalten Sie Zugriff auf:

  • über 102.000 Bücher
  • über 537 Zeitschriften

aus folgenden Fachgebieten:

  • Automobil + Motoren
  • Bauwesen + Immobilien
  • Business IT + Informatik
  • Elektrotechnik + Elektronik
  • Energie + Nachhaltigkeit
  • Finance + Banking
  • Management + Führung
  • Marketing + Vertrieb
  • Maschinenbau + Werkstoffe
  • Versicherung + Risiko

Jetzt Wissensvorsprung sichern!

Springer Professional "Technik"

Online-Abonnement

Mit Springer Professional "Technik" erhalten Sie Zugriff auf:

  • über 67.000 Bücher
  • über 390 Zeitschriften

aus folgenden Fachgebieten:

  • Automobil + Motoren
  • Bauwesen + Immobilien
  • Business IT + Informatik
  • Elektrotechnik + Elektronik
  • Energie + Nachhaltigkeit
  • Maschinenbau + Werkstoffe




 

Jetzt Wissensvorsprung sichern!

Springer Professional "Wirtschaft"

Online-Abonnement

Mit Springer Professional "Wirtschaft" erhalten Sie Zugriff auf:

  • über 67.000 Bücher
  • über 340 Zeitschriften

aus folgenden Fachgebieten:

  • Bauwesen + Immobilien
  • Business IT + Informatik
  • Finance + Banking
  • Management + Führung
  • Marketing + Vertrieb
  • Versicherung + Risiko




Jetzt Wissensvorsprung sichern!

Fußnoten
1
You can use emacs, vim, more, or less on Unix and Mac. On Windows, WordPad is appropriate, or the type command in a DOS window. Word processors such as OpenOffice or Microsoft Word can also be used.
 
2
The difference between these two outputs is explained in Chapter 7.​3.​9.
 
4
Choose “view as details” in Windows Explorer, “View as List” in Nautilus, the list view icon in Finder, or you just point at a file icon in Konqueror and watch the pop-up text.
 
5
If you use ls -l, the size measured in bytes is found in column 5, right before the date.
 
6
Otherwise, the output would have occupied several pages in this book with about five thousand backslash-x-number symbols…
 
7
The exact size depends on the complexity of the music, the length of the track, and the MP3 resolution.
 
8
From the assumption that there are always some strange people doing strange things, there might be somebody out there who – with a lot of training – can interpret the pure PDF code with the eyes.
 
9
Interpreting such files is much more complicated than our examples on reading human-readable files in this book.
 
10
Such objects are known as immutable data types and consist of int, float, complex, str, and tuple. Lists and dictionaries can change their contents and are called mutable objects. These cannot be used as keys in dictionaries. If you desire a list as key, use a tuple instead.
 
11
This is not strictly correct, because sum is a local variable while the summation function is associated with a global name sum, which can be reached through globals()[’sum’].
 
12
We could, alternatively, set the value to None.
 
13
Take a “break” and convince yourself that you understand how we specify the substring to be replaced.
 
14
In principle this is true, but in practice the text in web pages tend to be much more complicated than the text in the files we have treated so far.
 
15
Another term is URI (Uniform Resource Identifier), which is replacing URL in technical documentation. We stick to URL, however, in this book because Python’s tools for accessing resources on the Internet have url as part of module and function names.
 
16
prog can be any program, including a Python program run as, e.g., python myprog.py.
 
17
More precisely, a line y=ax+b is a “best fit” to the data points (x i ,y i ), i=0,…,n−1 if a and b are chosen to make the sum of squared errors \(R=\sum_{j=0}^{n-1} (y_{j} - (ax_{j} + b))^{2}\) as small as possible. This approach is known as least squares approximation to data and proves to be extremely useful throughout science and technology.
 
Metadaten
Titel
Files, Strings, and Dictionaries
verfasst von
Hans Petter Langtangen
Copyright-Jahr
2012
Verlag
Springer Berlin Heidelberg
DOI
https://doi.org/10.1007/978-3-642-30293-0_6