•        Python : 3.7.0
  •          OS : Ubuntu 18.04.1 LTS
  •         IDE : PyCharm 2018.2.4
  •       Conda : 4.5.11
  •    typesetting : Markdown

txt(utf-8)

《道德经》原文 "我有三宝持而保之∶一曰慈,二曰俭,三曰不敢为天下先。"
一曰慈,
二曰俭,
三曰不敢为天下先。

code

"""
@Author : 行初心
@Date : 18-10-2
@Blog : www.cnblogs.com/xingchuxin
@Gitee : gitee.com/zhichengjiu
""" def main():
# file 文件类型的对象
# 1.txt 的编码方式是 utf-8
with open(r'1.txt', encoding="utf-8") as file:
# 显示文件指针
print(file.tell()) # 这句话会改变文件的指针
lines = list(file)
print(lines) # 显示文件指针
print(file.tell()) print() # 这句话 非加不可。为啥呀?
# 因为在lines=list(file)处,文件指针被移动到了末尾
file.seek(0, 0) for eachLine in file:
print(eachLine) if __name__ == '__main__':
main()

result

/home/coder/anaconda3/envs/py37/bin/python /home/coder/PycharmProjects/base/demo.py
0
['《道德经》原文 "我有三宝持而保之∶一曰慈,二曰俭,三曰不敢为天下先。"\n', '一曰慈,\n', '二曰俭,\n', '三曰不敢为天下先。']
148 《道德经》原文 "我有三宝持而保之∶一曰慈,二曰俭,三曰不敢为天下先。" 一曰慈, 二曰俭, 三曰不敢为天下先。 Process finished with exit code 0

resource

  • [文档] docs.python.org/3
  • [规范] www.python.org/dev/peps/pep-0008
  • [规范] zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_language_rules
  • [源码] www.python.org/downloads/source
  • [ PEP ] www.python.org/dev/peps
  • [平台] www.cnblogs.com
  • [平台] gitee.com


Python具有开源、跨平台、解释型、交互式等特性,值得学习。

Python的设计哲学:优雅,明确,简单。提倡用一种方法,最好是只有一种方法来做一件事。

代码的书写要遵守规范,这样有助于沟通和理解。

每种语言都有独特的思想,初学者需要转变思维、踏实践行、坚持积累。

Python3基础 file for+文件指针 读取txt文本并 一行一行的输出(高效率)的更多相关文章

  1. java 文件操作 读取txt文本(兄弟常开心)

    测试一下读取文本的另一种方法:该方法只使用一个类读取了文件 注意:buffer和read方法中读取指定长度的一致 package com.swust; import java.io.*; /* * 数 ...

  2. 获取input type=file 的文件内容(纯文本)

    一.获取input type=file 的文件内容(纯文本) 1.需求一 通过点击其他事件,来触发 文件选择框(限定格式为 .c 文件),而不是手动鼠标点击触发. [思路:] step1:将 inpu ...

  3. Python3基础 file read 读取txt文件的前几个字符

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  4. Python3基础 file seek 将文件的指针恢复到初始位置

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  5. Python3基础 file for+list 读取txt文本 并 一行一行的输出(低效率)

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  6. Python3 中codecs进行文件的读取

    简单的概念与说明 编码(动词):按照某种规则(这个规则称为:编码(名词))将"文本"转换为"字节流".而在python 3中则表示:unicode变成str 解 ...

  7. C++ 读取txt文本内容,并将结果保存到新文本

    循序渐进学习读文件 // readFile.cpp : 定义控制台应用程序的入口点. #include "stdafx.h" #include <iostream> # ...

  8. c++ 按行读取txt文本

    CStdioFile 类的声明保存在 afx.h 头文件中. CStdioFile 类继承自 CFile 类, CStdioFile 对象表示一个用运行时的函数 fopen 打开的 c 运行时的流式文 ...

  9. C# 读取txt文本内容写入到excel

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

随机推荐

  1. Linux的/etc/services文件的作用?

    4)端口分配 Linux系统的端口号的范围为0–65535,不同范围有不同的意义. 0 不使用 1--1023 系统保留,只能由root用户使用 1024---4999 由客户端程序自由分配 5000 ...

  2. mysql 权限管理 针对库 授权 db.*

    需求 只放行user表 db1库的select权限 mysql> grant select on db1.* to 'mike'@'localhost'; Query OK, rows affe ...

  3. SQLCE数据工具(Flyhoward Ltd SDF Viewer)

    SDF Viewer sdf数据库创建编辑查看 官方下载地址  http://www.flyhoward.com/Download_SDF_Viewer.aspx 用户名:www.cr173.com注 ...

  4. 使用gradle构建多模块springboot项目,打jar包

    官方文档: https://spring.io/guides/gs/rest-service/  参考:http://blog.csdn.net/u013360850/article/details/ ...

  5. Selenium之Css Selector使用方法

    什么是Css Selector? Css Selector定位实际就是HTML的Css选择器的标签定位 工具 Css Selector的练习建议使用火狐浏览器,下载插件,FireFinder.Fire ...

  6. [vue]vue-cli下载原理

    正常vue-cli这样操作就ok了 vue-cli github $ npm install -g vue-cli $ vue init webpack my-project $ cd my-proj ...

  7. 调用sklearn包中的PLA算法[转载]

    转自:https://blog.csdn.net/u010626937/article/details/72896144#commentBox 1.Python的机器学习包sklearn中也包含了感知 ...

  8. pd.read_csv操作读取分隔符csv和text文件

    pandas.read_csv可以读取CSV(逗号分割)文件.文本类型的文件text.log类型到DataFrame 1. pandas.read_csv常用参数整理 也支持文件的部分导入和选择迭代 ...

  9. VMware coding Challenge: Coin Toss Betting

    static int CoinTossEndAmount(int betAmount, String coinTossResults) { if (betAmount <=0 || coinTo ...

  10. 转载如何实现portlet之间的传递参数

    Liferay 6开发学习(三十):跨页面Portlet之间的调用与数据传递 2014年10月09日 Liferay 评论 2 条 阅读 4,209 views 次 Portlet之间的通信方法有多种 ...