Skip to content

Commit ba2e754

Browse files
committed
文件处理
1 parent c8e0e80 commit ba2e754

1 file changed

Lines changed: 46 additions & 81 deletions

File tree

Notes/01_Introduction/06_Files.md

Lines changed: 46 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,91 @@
11
[Contents](../Contents.md) \| [Previous (1.5 Lists)](05_Lists.md) \| [Next (1.7 Functions)](07_Functions.md)
22

3-
# 1.6 File Management
3+
# 1.6 文件处理
44

5-
Most programs need to read input from somewhere. This section discusses file access.
5+
大多数程序需要从某些地方读取输入的数据。本节将讨论文件的访问。
66

7-
### File Input and Output
7+
### 文件的输入和输出
88

9-
Open a file.
9+
打开一个文件
1010

1111
```python
12-
f = open('foo.txt', 'rt') # Open for reading (text)
13-
g = open('bar.txt', 'wt') # Open for writing (text)
12+
f = open('foo.txt', 'rt') # 打开文件来读
13+
g = open('bar.txt', 'wt') # 打开文件来写
1414
```
1515

16-
Read all of the data.
16+
读取所有的数据
1717

1818
```python
1919
data = f.read()
2020

21-
# Read only up to 'maxbytes' bytes
21+
# 最多读取 'maxbytes' 字节数据
2222
data = f.read([maxbytes])
2323
```
2424

25-
Write some text.
25+
写入一些数据
2626

2727
```python
2828
g.write('some text')
2929
```
3030

31-
Close when you are done.
31+
完成后关闭
3232

3333
```python
3434
f.close()
3535
g.close()
3636
```
3737

38-
Files should be properly closed and it's an easy step to forget.
39-
Thus, the preferred approach is to use the `with` statement like this.
38+
文件应正确关闭,但这是一个容易忘记的步骤。因此,首选的方法是下面这样使用 with 语句
4039

4140
```python
4241
with open(filename, 'rt') as file:
43-
# Use the file `file`
42+
# 使用 `file`
4443
...
45-
# No need to close explicitly
44+
# 无需显式的关闭
4645
...statements
4746
```
4847

49-
This automatically closes the file when control leaves the indented code block.
48+
当离开 with 缩进的代码块时,会自动关闭文件。
5049

51-
### Common Idioms for Reading File Data
50+
### 读取文件的习惯用法
5251

53-
Read an entire file all at once as a string.
52+
以字符串形式一次读取整个文件
5453

5554
```python
5655
with open('foo.txt', 'rt') as file:
5756
data = file.read()
58-
# `data` is a string with all the text in `foo.txt`
57+
# `data` 是一个字符串,包含 `foo.txt` 文件的所有内容
5958
```
6059

61-
Read a file line-by-line by iterating.
60+
通过迭代逐行读取数据
6261

6362
```python
6463
with open(filename, 'rt') as file:
6564
for line in file:
66-
# Process the line
65+
# 在这里处理每行数据
6766
```
6867

69-
### Common Idioms for Writing to a File
68+
### 写入文件的习惯用法
7069

71-
Write string data.
70+
写入字符串数据
7271

7372
```python
7473
with open('outfile', 'wt') as out:
7574
out.write('Hello World\n')
7675
...
7776
```
7877

79-
Redirect the print function.
78+
重定向打印功能
8079

8180
```python
8281
with open('outfile', 'wt') as out:
8382
print('Hello World', file=out)
8483
...
8584
```
8685

87-
## Exercises
86+
## 练习
8887

89-
These exercises depend on a file `Data/portfolio.csv`. The file
90-
contains a list of lines with information on a portfolio of stocks.
91-
It is assumed that you are working in the `practical-python/Work/`
92-
directory. If you're not sure, you can find out where Python thinks
93-
it's running by doing this:
88+
这些练习依赖于文件 `Data/portfolio.csv`。该文件包含行列表以及有关股票投资组合的信息。假设你正在 `practical-python/Work/` 目录中工作。如果不确定运行位置,可以通过执行以下操作找出 Python 的运行位置:
9489

9590
```python
9691
>>> import os
@@ -99,9 +94,9 @@ it's running by doing this:
9994
>>>
10095
```
10196

102-
### Exercise 1.26: File Preliminaries
97+
### 练习 1.26:文件练习准备工作
10398

104-
First, try reading the entire file all at once as a big string:
99+
首先,将文件作为大字符串一次性读取。
105100

106101
```python
107102
>>> with open('Data/portfolio.csv', 'rt') as f:
@@ -121,18 +116,11 @@ name,shares,price
121116
>>>
122117
```
123118

124-
In the above example, it should be noted that Python has two modes of
125-
output. In the first mode where you type `data` at the prompt, Python
126-
shows you the raw string representation including quotes and escape
127-
codes. When you type `print(data)`, you get the actual formatted
128-
output of the string.
119+
在上面的示例中,应注意 Python 具有两种输出模式。在第一种模式下,你在提示符下键入 `data`,Python 向你显示原始字符串的表示形式,包括引号和转义码。当你键入 `print(data)` 时,你将获得字符串实际的格式化输出。
129120

130-
Although reading a file all at once is simple, it is often not the
131-
most appropriate way to do it—especially if the file happens to be
132-
huge or if contains lines of text that you want to handle one at a
133-
time.
121+
尽管一次读取一个文件很简单,但通常不是最合适的处理方式 —— 尤其是当文件碰巧很大或包含需要一次处理一个的文本行时。
134122

135-
To read a file line-by-line, use a for-loop like this:
123+
要逐行读取文件,请使用如下所示的for循环:
136124

137125
```python
138126
>>> with open('Data/portfolio.csv', 'rt') as f:
@@ -146,12 +134,9 @@ name,shares,price
146134
>>>
147135
```
148136

149-
When you use this code as shown, lines are read until the end of the
150-
file is reached at which point the loop stops.
137+
当你用如上所示的代码时,将按行读取,直到文件末尾,然后循环停止。
151138

152-
On certain occasions, you might want to manually read or skip a
153-
*single* line of text (e.g., perhaps you want to skip the first line
154-
of column headers).
139+
在某些情况下,你可能希望手动读取或跳过一行文本(例如:你可能想跳过第一行的列标题数据。)
155140

156141
```python
157142
>>> f = open('Data/portfolio.csv', 'rt')
@@ -168,12 +153,9 @@ of column headers).
168153
>>>
169154
```
170155

171-
`next()` returns the next line of text in the file. If you were to call it repeatedly, you would get successive lines.
172-
However, just so you know, the `for` loop already uses `next()` to obtain its data.
173-
Thus, you normally wouldn’t call it directly unless you’re trying to explicitly skip or read a single line as shown.
156+
`next()` 用来返回文件中的下一行文本。如果反复的调用它,则会得到连续的行。然而,就像你知道的,我们已经在 `for` 循环中使用 `next()` 来获取其数据。因此,除非你视图显式的跳过或者读取一行,否者通常不会直接调用它。
174157

175-
Once you’re reading lines of a file, you can start to perform more processing such as splitting.
176-
For example, try this:
158+
读取文件行后,你可以执行更多的处理操作,例如进行拆分。尝试以下操作:
177159

178160
```python
179161
>>> f = open('Data/portfolio.csv', 'rt')
@@ -190,59 +172,42 @@ For example, try this:
190172
>>> f.close()
191173
```
192174

193-
*Note: In these examples, `f.close()` is being called explicitly because the `with` statement isn’t being used.*
175+
*注意:在上面这些例子中,因为没有使用 `with` 语句,所以显式调用了 `f.close()` *
194176

195-
### Exercise 1.27: Reading a data file
177+
### 练习 1.27:读取一个数据文件
196178

197-
Now that you know how to read a file, let’s write a program to perform a simple calculation.
179+
现在你已经学会了如何读取文件,让我们来编写一个程序来执行简单的计算。
198180

199-
The columns in `portfolio.csv` correspond to the stock name, number of
200-
shares, and purchase price of a single stock holding. Write a program called
201-
`pcost.py` that opens this file, reads all lines, and calculates how
202-
much it cost to purchase all of the shares in the portfolio.
181+
`portfolio.csv` 中的列对应于股票名称,股票数量和单股股票的购买价格。编写一个名为`pcost.py` 的程序,打开该文件,读取所有行,并计算购买所有股票的费用。
203182

204-
*Hint: to convert a string to an integer, use `int(s)`. To convert a string to a floating point, use `float(s)`.*
183+
*提示:将字符串转换为整数,请使用 `int(s)`。转换为浮点数,请使用`float(s)`*
205184

206-
Your program should print output such as the following:
185+
你的程序应该会打印如下所示的内容:
207186

208187
```bash
209188
Total cost 44671.15
210189
```
211190

212-
### Exercise 1.28: Other kinds of "files"
191+
### 练习 1.28:其他类型的“文件”
213192

214-
What if you wanted to read a non-text file such as a gzip-compressed
215-
datafile? The builtin `open()` function won’t help you here, but
216-
Python has a library module `gzip` that can read gzip compressed
217-
files.
193+
如果您想读取非文本文件,如 gzip 压缩的数据文件,应该怎么办?内置的 `open()` 函数在这里无济于事,但是 Python 有一个模块库 `gzip` ,可以读取 gzip 压缩文件。
218194

219-
Try it:
195+
来尝试一下:
220196

221197
```python
222198
>>> import gzip
223199
>>> with gzip.open('Data/portfolio.csv.gz', 'rt') as f:
224200
for line in f:
225201
print(line, end='')
226202

227-
... look at the output ...
203+
... 查看输出内容 ...
228204
>>>
229205
```
230206

231-
Note: Including the file mode of `'rt'` is critical here. If you forget that,
232-
you'll get byte strings instead of normal text strings.
207+
注意:在这里`'rt'` 的文件模式至关重要。如果您忘记了这一点,将获得字节字符串而不是普通的文本字符串。
233208

234-
### Commentary: Shouldn't we being using Pandas for this?
209+
### 评论:为什么我们不使用 Pandas 呢?
235210

236-
Data scientists are quick to point out that libraries like
237-
[Pandas](https://pandas.pydata.org) already have a function for
238-
reading CSV files. This is true--and it works pretty well.
239-
However, this is not a course on learning Pandas. Reading files
240-
is a more general problem than the specifics of CSV files.
241-
The main reason we're working with a CSV file is that it's a
242-
familiar format to most coders and it's relatively easy to work with
243-
directly--illustrating many Python features in the process.
244-
So, by all means use Pandas when you go back to work. For the
245-
rest of this course however, we're going to stick with standard
246-
Python functionality.
211+
数据科学家很快指出,像 [Pandas](https://pandas.pydata.org) 这样的库已经具有读取 CSV 文件的功能。的确如此-而且效果很好。但是,这不是学习 Pandas 的课程。读取文件是比读取特定的 CSV 文件更普遍的问题。我们使用 CSV 文件的主要原因是,它是大多数开发者熟悉的格式,并且相对容易说明此过程中的许多 Python 特性。这就意味着,当你回去工作时,还是应该使用 Pandas。但是,在本课程的其余部分中,我们将继续使用标准 Python 功能。
247212

248213
[Contents](../Contents.md) \| [Previous (1.5 Lists)](05_Lists.md) \| [Next (1.7 Functions)](07_Functions.md)

0 commit comments

Comments
 (0)