提交 0eeb5772 编写于 作者: W wizardforcel

2019-11-18 18:10:04

上级 19fe3c5a
# 读取文件
> 原文: [https://pythonspot.com/read-file/](https://pythonspot.com/read-file/)
您之前已经看过各种类型的数据持有者:整数,字符串,列表。 但是到目前为止,我们还没有讨论如何读取或写入文件。
## 读取文件
You can read a file with the code below.
该文件必须与程序位于同一目录中,如果不是,则需要指定路径。
```py
#!/usr/bin/env python
# Define a filename.
filename = "bestand.py"
# Open the file as f.
# The function readlines() reads the file.
with open(filename) as f:
content = f.readlines()
# Show the file contents line by line.
# We added the comma to print single newlines and not double newlines.
# This is because the lines contain the newline character '\n'.
for line in content:
print(line),
```
代码的第一部分将读取文件内容。 读取的所有行将存储在变量内容中。 第二部分将遍历变量内容中的每一行。
如果您不想读取换行符“ \ n”,则可以将语句 f.readlines()更改为此:
```py
content = f.read().splitlines()
```
产生此代码:
```py
#!/usr/bin/env python
# Define a filename.
filename = "bestand.py"
# Open the file as f.
# The function readlines() reads the file.
with open(filename) as f:
content = f.read().splitlines()
# Show the file contents line by line.
# We added the comma to print single newlines and not double newlines.
# This is because the lines contain the newline character '\n'.
for line in content:
print(line)
```
当上面的代码起作用时,我们应该始终测试要打开的文件是否存在。 我们将首先测试文件是否不存在,如果存在,它将读取文件,否则返回错误。 如下面的代码:
```py
#!/usr/bin/env python
import os.path
# Define a filename.
filename = "bestand.py"
if not os.path.isfile(filename):
print('File does not exist.')
else:
# Open the file as f.
# The function readlines() reads the file.
with open(filename) as f:
content = f.read().splitlines()
# Show the file contents line by line.
# We added the comma to print single newlines and not double newlines.
# This is because the lines contain the newline character '\n'.
for line in content:
print(line)
```
[下载 Python 练习](https://pythonspot.com/download-python-exercises/)
\ No newline at end of file
# 写文件
> 原文: [https://pythonspot.com/write-file/](https://pythonspot.com/write-file/)
Python 默认支持写文件,不需要特殊模块。 您可以使用.write()方法以及包含文本数据的参数来写入文件。
在将数据写入文件之前,请调用 open(filename,’w’)函数,其中 filename 包含文件名或文件名的路径。 最后,别忘了关闭文件。
## 创建要写入的文件
The code below creates a new file (or overwrites) with the data.
```py
#!/usr/bin/env python
# Filename to write
filename = "newfile.txt"
# Open the file with writing permission
myfile = open(filename, 'w')
# Write a line to the file
myfile.write('Written with Python\n')
# Close the file
myfile.close()
```
“ w”标志可使 Python 截断该文件(如果已存在)。 也就是说,如果文件内容存在,它将被替换。
## 附加到文件
If you simply want to add content to the file you can use the ‘a’ parameter.
```py
#!/usr/bin/env python
# Filename to append
filename = "newfile.txt"
# The 'a' flag tells Python to keep the file contents
# and append (add line) at the end of the file.
myfile = open(filename, 'a')
# Add the line
myfile.write('Written with Python\n')
# Close the file
myfile.close()
```
## 参数
A summary of parameters:
[下载 Python 练习](https://pythonspot.com/download-python-exercises/)
| 旗 | 行动 |
| --- | --- |
| [R | 打开文件以供阅读 |
| w | 打开文件进行写入(将截断文件) |
| b | 二进制更多 |
| r + | 打开文件进行读写 |
| a + | 打开文件进行读写(附加到结尾) |
| w + | 打开文件进行读写(截断文件) |
\ No newline at end of file
......@@ -104,6 +104,3 @@ print jobs
大多数电子表格或 Office 程序都可以导出 csv 文件,因此我们建议您创建任何类型的 csv 文件并进行处理:-)
## 相关课程
[使用 Python 熊猫进行数据分析](https://gum.co/KmxqY)
\ No newline at end of file
......@@ -19,7 +19,7 @@ One of these database management systems (DBMS) is called SQLite. SQLite was cre
SQL is a special-purpose programming language designed for managing data held in a [databases](https://pythonspot.com/python-database/). The language has been around since 1986 and is worth learning. The [is an old funny video about SQL](https://www.youtube.com/watch?v=5ycx9hFGHog)
## SQLite
## SQLite
![SQLite](img/b49bf74e6651a4fea2a00aefd48f3b71.jpg)
......
# 带有 SqlAlchemy 的 ORM
# SqlAlchemy 和 ORM
> 原文: [https://pythonspot.com/orm-with-sqlalchemy](https://pythonspot.com/orm-with-sqlalchemy)
......
......@@ -21,7 +21,7 @@ A framework may offer some or all of these features.
**相关课程:** [使用 Python Flask 创建网络应用](https://gum.co/IMzBy)
## 为什么要使用网络框架?
## 为什么要使用 Web 框架?
As you are doing web development, you want to avoid spending time on programming things that have already been solved. On the other hand, if you are an experienced web developer a web framework may not offer everything you need.
......@@ -30,7 +30,7 @@ As you are doing web development, you want to avoid spending time on programming
Django and Flask are the most popular web frameworks. However, you may want to evaluate the frameworks. An overview:
* [Django](https://www.djangoproject.com/)
* [烧瓶](http://flask.pocoo.org/)
* [Flask](http://flask.pocoo.org/)
* [](http://bottlepy.org/docs/dev/index.html)
* [金字塔](http://www.pylonsproject.org/projects/pyramid/about)
* [松饼](https://github.com/klen/muffin)
......@@ -43,7 +43,7 @@ The most popular python web application framework is **Django**, followed by **F
[<picture><source srcset="/wp-content/uploads/2015/08/python-web-development.jpg.webp" type="image/webp"> <source srcset="/wp-content/uploads/2015/08/python-web-development.jpg" type="image/jpeg"> ![python web development](img/5a539b08d07773dbba46f659b5d1743c.jpg) </picture>](/wp-content/uploads/2015/08/python-web-development.jpg) 在 Github 上提及框架的项目数量。
## Django
## Django
![](img/2f129027ae983205309e3979ced402ab.jpg)
......@@ -64,7 +64,7 @@ The most popular python web application framework is **Django**, followed by **F
If you want to know more about Django, [read here.](https://pythonspot.com/django-tutorial-building-a-note-taking-app/)Did you know the websites of [NASA](https://www.nasa.gov/), [Bitbucket](https://bitbucket.org/) and [Pinterest](https://www.pinterest.com/) were made with Django?
## 烧瓶
## Flask
![flask-logo](img/2a47be594c5c80724c14a822dd96b251.jpg)
......
# 烧瓶入门:Hello World
# Flask 入门:Hello World
> 原文: [https://pythonspot.com/flask-hello-world/](https://pythonspot.com/flask-hello-world/)
......@@ -12,7 +12,7 @@
* [minitwit](https://github.com/mitsuhiko/flask/tree/master/examples/minitwit/) -一个推特克隆
* [flask 网站](https://github.com/mitsuhiko/flask-website)-静态页面+邮件列表文件
## 安装烧瓶
## 安装 Flask
使用以下命令安装 Flask:
......@@ -49,4 +49,4 @@ $ python hello.py
在您的网络浏览器中打开 [http:// localhost:5000 /](http://localhost:5000/) ,然后会出现“ Hello World!”。
[下载烧瓶示例](https://pythonspot.com/download-flask-examples/)
\ No newline at end of file
[下载 Flask 示例](https://pythonspot.com/download-flask-examples/)
\ No newline at end of file
# 带 Python 的 Flask Web 应用程序(入门教程)
# Python 和 Flask Web 应用程序(入门教程)
> 原文: [https://pythonspot.com/flask-web-app-with-python/](https://pythonspot.com/flask-web-app-with-python/)
......@@ -8,7 +8,7 @@ Python app created with Flask
在本教程中,您将学习如何使用 Python 构建网络应用。 我们将使用称为 Flask 的微型框架。
## 为什么要装瓶
## 为什么是 Flask
* 易于使用。
......@@ -89,7 +89,7 @@ $ python hello.py
python-flask-webapp
## 样式烧瓶页面
## Flask 页面模板
We will separate code and User Interface using a technique called Templates. We make the directory called /templates/ and create the template:
......@@ -122,7 +122,7 @@ if __name__ == "__main__":
You can then open : [http://127.0.0.1/hello/Jackson/](http://127.0.0.1/hello/Jackson/)
## Styling the template
## 给模板添加样式
Do you want some better looking template? We modify the file:
......
# 使用 Flask 登录验证
# Flask 和登录验证
> 原文: [https://pythonspot.com/login-authentication-with-flask/](https://pythonspot.com/login-authentication-with-flask/)
......@@ -8,7 +8,7 @@ The Flask Logo
在本教程中,您将学习如何使用 [Flask](https://pythonspot.com/en/python-flask-tutorials/) 和 Python 构建登录 Web 应用程序。
### 建立一个 Flask 登录屏幕
### 建立一个 Flask 登录页面
Create this Python file and save it as app.py:
......
......@@ -10,7 +10,7 @@
Don’t worry if that sounds vague, we’ll take you through the steps.
## 与 Google 的登录身份验证
## 使用 Google 的登录验证
我们使用名为 **flask_oauth** 的模块向 Google 进行身份验证。 它由 Flask 的创建者 Armin Ronacher 维护,因此可以确保该模块不会消失。 该模块使用 OAuth,该协议提供令牌以访问资源。 其他模块可能没有很好的支持。
......
# 用 Twitter 登录 Flask 应用
# 使用 Twitter 登录 Flask 应用
> 原文: [https://pythonspot.com/login-to-flask-app-with-twitter/](https://pythonspot.com/login-to-flask-app-with-twitter/)
......
......@@ -31,7 +31,7 @@ print rates
```
## 带有 Flask 的 Google Charts API
## 使用 Flask 的 Google Charts API
With the [Google Charts API](https://developers.google.com/chart/interactive/docs/gallery) you can display live data on your site. There are a lot of great charts there that are easy to add to your Flask app. We simply give the data that we got from the server through JSON and parsed, to the Google Charts API.
......
# 烧瓶网页表单
# Flask 网页表单
> 原文: [https://pythonspot.com/flask-web-forms/](https://pythonspot.com/flask-web-forms/)
......@@ -10,7 +10,7 @@ Flask web form
我们使用 WTForms,这是用于验证表单的模块。 我们将从一个简单的表格开始,其中包含一个要求输入名称的字段。
## CSS 与烧瓶
## CSS 与 Flask
We use bootstrap to style the form.Bootstrap is a popular HTML, CSS, and JS framework for developing responsive, mobile first projects on the web. It makes front-end web development faster and easier. The output will be:
......@@ -92,7 +92,7 @@ if __name__ == "__main__":
```
## 烧瓶登记
## Flask 注册
We use the same principle to create a registration form asking for name, email and password. We update the Form class:
......
# 带有静态 html 文件的烧瓶
# Flask 和静态 html 文件
> 原文: [https://pythonspot.com/flask-with-static-html-files/](https://pythonspot.com/flask-with-static-html-files/)
......@@ -8,9 +8,6 @@ Flask 将为您提供 URL 路由,许多功能以及所有 Python 好处。
您可能需要一个部分动态和部分静态的应用程序。 或者,您可能只想使用 URL 路由进行浏览。 在本文中,我们将教您如何使用 Flask 加载静态 HTML 文件。
## 相关课程
[Python Flask:使用 Python 制作 Web 应用](https://gum.co/IMzBy)
```py
from flask import Flask, render_template
......
# 带引导程序的烧瓶样板,SQLAlchemy
# Flask 模板,SQLAlchemy 和 Bootstarp
> 原文: [https://pythonspot.com/flask-boilerplate-with-bootstrap-sqlalchemy/](https://pythonspot.com/flask-boilerplate-with-bootstrap-sqlalchemy/)
在本文中,您将学习 cookiecutter,这是一个从项目模板创建项目的命令行实用程序。 使用 cookiecutter,您可以创建一个新的 python Flask 项目。 这类似于标准的 Flask 项目,除了使用此方法时,您将从几个完整的模板和功能列表开始。
## 相关课程:
[Python Flask:使用 Python 制作 Web 应用](https://gum.co/IMzBy)
功能
* 具有入门模板的 Bootstrap 3 和 Font Awesome 4
......
# 使用 Chart.js 的烧瓶和美观的图表
# Flask 和使用 Chart.js 的美观的图表
> 原文: [https://pythonspot.com/flask-and-great-looking-charts-using-chart-js/](https://pythonspot.com/flask-and-great-looking-charts-using-chart-js/)
......
# 使用 Python 进行 JSON 编码和解码
# Python 和 JSON 编码和解码
> 原文: [https://pythonspot.com/json-encoding-and-decoding-with-python/](https://pythonspot.com/json-encoding-and-decoding-with-python/)
......@@ -55,7 +55,7 @@ for element in data['drinks']:
```
## 将 JSON 转换为 Python 对象(浮点
## 将 JSON 转换为 Python 对象(浮点)
浮点数可以使用十进制库进行映射。
......@@ -123,7 +123,7 @@ print json.dumps(d, ensure_ascii=False)
| 假 | 假 |
| 空值 | 没有 |
## 漂亮的印刷
## 漂亮的打印
If you want to display JSON data you can use the json.dumps() function.
......
......@@ -22,7 +22,7 @@ sudo apt-get install python-kde4
```
## 使用 PyQT 创建 GUI
## 使用 PyQt 创建 GUI
Start qt4-designer from your applications menu. The [QT](https://pythonspot.com/en/pyqt4/) Designer application will appear:![QT_Designer](img/c5c710d4eea36d37879737f96ff29bc9.jpg)
......@@ -37,7 +37,7 @@ pyuic4 browser.ui > browser.py
这将生成一个 Python 文件。 从 browser.py 文件底部删除“ from kwebview import KWebView”行。 将 KWebView 更改为 QtWebView。 我们想改用 QtWebView。 如果您不愿意进行更改,请从下面获取 browser.py 文件。
## QWebView 探索
## `QWebView`探索
Create a file called** _r_**_**un.py**_ with this contents:
......@@ -93,13 +93,13 @@ python run.py
请确保输入完整的网址,例如 : [http://pythonspot.com](https://pythonspot.com) 包括 http://部分。 您的浏览器现在应该启动:
<caption id=”attachment_295” align=”alignnone” width=”1026”]![Python browser](img/287d5ac2bd36de412e26bc9ed4a3f4c9.jpg)
![Python browser](img/287d5ac2bd36de412e26bc9ed4a3f4c9.jpg)
Python browser
如果您的代码未运行,请使用以下代码(或查看不同之处并更改错误之处):
## browser.py
## `browser.py`
```py
......@@ -151,7 +151,7 @@ Dialog.setWindowTitle(_translate("Browser", "Browser", None))
```
## run.py
## `run.py`
```py
......
# 创建一个 gmail wordcloud
# 创建一个 gmail 词云
> 原文: [https://pythonspot.com/creating-a-gmail-wordcloud/](https://pythonspot.com/creating-a-gmail-wordcloud/)
......
......@@ -2,7 +2,7 @@
> 原文: [https://pythonspot.com/django-tutorial-building-a-note-taking-app/](https://pythonspot.com/django-tutorial-building-a-note-taking-app/)
## Django
## Django
If you want to start with python web development, you could use a web framework named Django. It is designed to be fast, secure and scalable. It comes with an object-relational mapper (ORM), which means that objects in Python are mapped to objects in a database.
......@@ -59,7 +59,7 @@ This will create the directory mysite. Open mysite/mysite/settings.py. You can c
在网络浏览器中打开 [http://127.0.0.1:8000](http://127.0.0.1:8000) ,您应该看到:
<caption id=”attachment_1710” align=”alignnone” width=”937”]![django](img/2b32675c9f6d4fd2f2bb157ad1dbb461.jpg)
![django](img/2b32675c9f6d4fd2f2bb157ad1dbb461.jpg)
Our first Django app.
......@@ -243,7 +243,7 @@ Once you fire up your browser you will see the list of notes:
django app
## 插入资料
## 插入数据
While it’s nice to have a list, we want to add some notes to it.
Create the file /mysite/notes/forms.py
......@@ -309,7 +309,7 @@ Run it and we have our note taking app :-)
Djano note taking app
## 造型应用
## 为应用添加样式
By modifying the note.html we can style it like any other html/css website. If you change note.html to:
......@@ -335,6 +335,6 @@ By modifying the note.html we can style it like any other html/css website. If y
You will get:
<caption id=”attachment_1729” align=”alignnone” width=”1189”]![django_note_app](img/18212430a11e21ca680a2bbd5ca295f1.jpg)
![django_note_app](img/18212430a11e21ca680a2bbd5ca295f1.jpg)
Django note taking app
\ No newline at end of file
# 请求:HTTP for Humans
# Requests:人性化的 HTTP
> 原文: [https://pythonspot.com/requests-http-for-humans/](https://pythonspot.com/requests-http-for-humans/)
......
# 使用 Python 的 HTTP 下载文件
# Python 和 HTTP 下载文件
> 原文: [https://pythonspot.com/http-download-file-with-python/](https://pythonspot.com/http-download-file-with-python/)
......@@ -6,7 +6,7 @@
在本文中,您将学习如何使用 Python 从 Web 下载数据。
## 下载文
## 下载文
To download a plain text file use this code:
......
# HTTP-解析 HTML 和 XHTML
# HTTP解析 HTML 和 XHTML
> 原文: [https://pythonspot.com/http-parse-html-and-xhtml](https://pythonspot.com/http-parse-html-and-xhtml)
在本文中,您将学习如何解析网站的 HTML(超文本标记语言)。 有几个 Python 库可以实现这一目标。 我们将演示一些流行的例子。
## Beautiful Soup-用于解析 HTML 和 XML 的 python 包
## Beautiful Soup用于解析 HTML 和 XML 的 python 包
该库非常流行,甚至可以处理格式错误的标记。 要获取单个 div 的内容,可以使用以下代码:
......@@ -67,7 +67,7 @@ for link in links:
```
## PyQuery-一个类似 Python 的 jquery
## PyQuery:一个类似 jquery 的 Python
要从标记中提取数据,我们可以使用 PyQuery。 它可以根据您的需要获取实际的文本内容和 html 内容。 要获取标签,请使用调用 pq('tag')。
......@@ -95,7 +95,7 @@ tag = pq('title')
```
## HTMLParser-简单的 HTML 和 XHTML 解析器
## HTMLParser简单的 HTML 和 XHTML 解析器
该库的用法非常不同。 使用此库,您必须将所有逻辑都放在 WebParser 类中。 用法的基本示例如下:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册