查看current工作路径:

 >>> import os
>>> os.getcwd()
'D:\\python'

更改工作路径:

 >>> os.chdir('E:\\forpython') # 要用双斜杠

读入文件:

 >>> red_data = open('《红楼梦》.txt')
>>> print(red_data.readline())
《红楼梦》
>>> print(red_data.readline())
曹雪芹  高鄂 著
>>>print(red_data.readline())
第一回 甄士隐梦幻识通灵 贾雨村风尘怀闺秀
>>> red_data.seek(0)
0
>>> print(red_data.readline())
《红楼梦》

刷屏了。。。

 >>> for each_line in red_data:
print(red_data.readline(),end = '\t')

处理完了之后:

 >>> red_data.close()

对字符串的split:

 >>> test = 'man said:\'how are you\''
>>> test
"man said:'how are you'"
>>> (role,line_spoken) = test.split(":") # 多重赋值
>>> role
'man said'
>>> line_spoken
"'how are you'"

head frist Python里面文件的资源链接:http://python.itcarlow.ie/resources.html

查看BulitInFunction的帮助文档:

 >>> help(test.split)

很奇怪,用help(split)会报错。。。这个有待研究

 >>> help("split")
No Python documentation found for 'split'.
Use help() to get the interactive help utility.
Use help(str) for help on the str class.

看看help()会有什么结果:

 >>> help()

 Welcome to Python 3.6's help utility!

 If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/3.6/tutorial/. Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit". To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics". Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam". help>
 help> split
No Python documentation found for 'split'.
Use help() to get the interactive help utility.
Use help(str) for help on the str class.

没办法。。。去百度一下吧

 help("open")

这样用在split上还是会报错。。。不管了

打开在headfirst Python support site下载的sketch.txt,跑一下程序:

 >>> data = open('sketch.txt')
>>> for each_line in data:
(role,line_spoken) = each_line.split(':',)
print(role,end=' ')
print(' said:',end=' ')
print(line_spoken,end=' ')
>>> data.close()

打印一部分后会报错:

Traceback (most recent call last):
  File "<pyshell#14>", line 2, in <module>
    (role,line_spoken) = each_line.split(':',1)
ValueError: not enough values to unpack (expected 2, got 1)

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUMAAABXCAYAAACeAyc+AAAMLElEQVR4nO2dX4gcRR7Hf907E5NV/BfNPxUTjYqXlzPJeSQxL8ELJoIivopBjS8+BH0VxAdBQRHk8iQYzoj4IOKDmOzbHSxK4KKckIso/llB3YiLJp5/YtzZ6evq3t7tra2qru6q6q7u+X5gmJme7l/9uqbmO/X3V8Gx/5yJ9v95HTGCIKAoiohHdLzqubLrdGzrkF3LP6vsF/ln4o+OHdvpq+zp5n+eKvdeNc90799GWQUgT69pB+pmlH4YVe8VYgJGkTD/JqtJ5anjh8Cn2QaYz2X9rit/RemoyJ/b9HfB+26ax7Lrq3x/oNssqxnyhUn2Q82fl2+SZc+yY6IfKn9u/nXZ5qHseGZHJhR82qp7MkGVvzr5VzatovRF55k0k02/Q97H7HW+26PIvup6AGQIm8m6hUZUCHVsqQTWhPz1efEzSceFr2VsuvgBq2za/A5s2ZG9rnJ9WTtgdBi5PsMuo1OjBwCIgRh2CAggANUJi08BoD1gYARUBWIIAADENZP5Pidf+6CKRmNNfHV5zzb8qyN91366tF92WhHwkyZG/5fNMxThmxBmU3RcCHZm1wVN52PT6bsGU2iACa1qJosKOmoCYkzyxOUfgkv7ZeyizDSLj/nfmBiioxsA4BOVptYUrSDxob+xjr4/mX3dFRwufNRdAVLU76rySfQnVqVGVta+jfJlY4WM6/Kvs0JLZyUTb0N0vzKfRdfr+qfrW9N96DzSFSii5U8MVVQQ2dInWfPWFVUitVS1zR8rGzXFdj6o/JD5wy9XzI7pXKs6t8i/svbLlC+d9Kvkva3yX9V+9l60ZFN0fdFSVNGfjCiP8npgcv+m+e+S0jXDosKcP6cpmk5fhyYLgysBdkWdf6RFuC7/Ovb5NOrOjzK+tYnKzeSiajmQo/qX9R1Z7aIt9m3guvyb2K+ji6qO76OJ77y0GLbxB+wbbc+/Mt0APto3wbUvpvZ9yqu20arRZN0+zLYgux+fa9quff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dFPyKdws8v37OFjsA2XUupIv5tpu3+g27QSPSBpgu/qObnE6r8ka0Vbwu6/rfpnkA3GLlQLF3omHcZpacO2u4/6CZLZhzr9rHZxreamQ51bDmQT0v0uo3o+l9n/gLAWFYz1B1JFU1dEUW14Y+pmkk2RzL54/kQYbIYjaLzXUTqUdnUaUaaNJN187jq1CQd+21u5oPuImwm6xZOnRHWMv1Bpj8K0aCLaX+UC1+Lri3qMzTBxr2bXFO1bAHgmvoW5gIAgMdADAEAgCCGnQMDDwBUA2IIAAAkiFqT0fT6XBGi0UkXPrYhxJUM2Wh5WTD3D4way+YZivDlRyEL6JB/HmVE66oBAHqM3AoUHXTExMd4f2XnA0I0AViksT5Dk45+W01BAADIqFQzbHs8QN1rfYv3ZyN/Tf0HoKtIV6D4HA+Q91HXP1272TVFn9cd769M/uqkDxEEYBGj4K5F5wA72P4jAQAsp3Iz2ed4gG3Gl3iFEFwwalTaA8WXH0pXB1IQ7w+A+mnlaHKX8SVP8P2AUUO6AoU/7kM8QJEt0chqV+P96cSL1KWq/wB0lSVi6Hs8QF1bLmMNmqZjO5ahq3uFQIJRA4EaAACAIIYAAJCgJYZt6khvk68AAH8oFMO2Tevo6nQbAIBblGLYNiHMgCACAMqCPkMAACCFGLa1VpiB2iEAoAyoGQIAAJVcmyyKp1cU8y9/jmoPk6LAD6rP21yDBQD4QSkxFMXT04mtx8c7FNksul7HPgAAVKVSCC9+2R6/Vphhs7+uaO0uAACY4mRDKJN4hzqBFlALBADYRiqGsrD6RdhostpoBqPpDAAog/FosqnoiPZYMbUBAABlUTaTVZsu5c8RXcMjG0SRDcrwNlX2MaACADClsM9QJIgmMflEn5exXXQOhBAAUAWtZrIoqrKvQAgBAFUoPc8QAAC6yP8BUOUYlxXBqIcAAAAASUVORK5CYII=" alt="" />因为这一段中没有':'

解决办法:

 >>> for each_line in data:
if not each_line.find(':') == -: # 返回要查找字符所在的位置
(role,line_spoken) = each_line.split(':',)
print(role,end=' ')
print(' said:',end=' ')
print(line_spoken,end=' ')

检查文件是否存在:

 >>> os.path.exists('123.xlsx')

异常错误处理机制:

 >>> for each_line in data:
try:
(role,line_spoken) = each_line.split(':',)
print(role,end=' ')
print(' said:',end=' ')
print(line_spoken,end=' ')
except:
pass

指定处理异常的类型:

>>> for each_line in data:
try:
(role,line_spoken) = each_line.split(':',)
print(role,end=' ')
print(' said:',end=' ')
print(line_spoken,end=' ')
except ValueError:
pass

分别将两个人的台词保存到两个列表中:

 >>> man=[]
>>> other_man=[]
>>> for each_line in data:
try:
(role,line_spoken)=each_line.split(':',)
line_spoken=line_spoken.strip()
if role=='Man':
man.append(line_spoken)
elif role=='Other Man':
other_man.append(line_spoken)
except ValueError:
pass

写出文件:

 >>> out1=open('man.txt','w')
>>> out2=open('other_man.txt','w')
>>> print(man,file=out1)
>>> print(other_man,file=out2)
>>> out1.close()
>>> out2.close()

完成写入后一定要关闭文件,即:out1.close() 这称为刷新输出

 >>> try:
man_file=open('man\'s word.txt','w')
other_file=open('other man\'s word.txt','w')
print(man,file=man_file)
print(other_man,file=other_file) except IOError:
print('File error')
finally:
man_file.close()
other_file.close()

使用finally确保文件的关闭

打印异常错误:

 except IOError as err:
print('File error:'+err)

使用with:

 >>> try:
with open('1.txt','w') as data1:
print(man,file=data1)
with open('2.txt','w') as data2:
print(other_man,file=data2)
except IOError as err:
print('file error: ',str(err))

不需要close了,这样代码更简洁

修改上次的print_it_all,

 >>> def print_it_all(thelist,indent=False,level=0,fn=sys.stdout):
for each_item in thelist:
if isinstance(each_item,list):
print_it_all(each_item,indent,level+1,fn)
else:
if indent:
for tap_stop in range(level):
print('\t',end='',file=fn)
print(each_item,file=fn)

再用print_it_all把数据输出到外部文件:

 >>> with open('aaa.txt','w') as mdf:
print_it_all(man,fn=mdf)

pickle:个人理解是对数据的封装(封装就是只能通过Python打开这个文件),并且持久的储存数据

 >>> with open('man_data.txt','wb') as man_file:     pickle.dump(man,man_file)

 >>> new_man=[]
>>> with open('man_data.txt','rb') as man_file:
new_man=pickle.load(man_file)

Python学习笔记:外部数据的输入、存储等操作的更多相关文章

  1. python学习笔记3_数据载入、存储及文件格式

    一.丛mysql数据库中读取数据 import pandas as pdimport pymysqlconn = pymysql.connect( host = '***', user = '***' ...

  2. Python学习笔记(15)- os\os.path 操作文件

    程序1 编写一个程序,统计当前目录下每个文件类型的文件数,程序实现如图: import os def countfile(path): dict1 = {} # 定义一个字典 all_files = ...

  3. Python学习笔记(三)字符串类型及其操作(2)

    1.字符串的表示 字符串是字符的序列表示,可以由一对单引号(‘).双引号(“)或三引号(’‘’)构成.其中,单引号.双引号和三引号都可以表示单行字符串,但是只有三引号可以表示多行字符串 在使用双引号时 ...

  4. Python学习笔记:数据的处理

    上次的学习中有个split函数,照着head first Python上敲一遍代码: >>> with open('james.txt') as jaf: data=jaf.read ...

  5. Python学习笔记(一)——输入与输出

    输出:——print() Python中的输出使用print()完成 >>> 在屏幕中输出Hello World >>> print('Hello World') ...

  6. python学习笔记七--数据操作符

    一.Python表达式操作符及程序:

  7. Android学习笔记之数据的Sdcard存储方法及操作sdcard的工具类

    FileService.java也就是操作sdcard的工具类: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...

  8. python学习笔记七--数据操作符的优先级

    一.混合表达式哪个部分先计算: 1. 取决于操作符的优先级,不同操作符的优先级是不一样的,例如‘*’的优先级高于‘+’ 2. 上节的表格里越靠后,优先级越高. 3 . 同一表达式的相同操作符是按从左到 ...

  9. ★★★★[转载]Python学习笔记一:数据类型转换★★★★

    一.int函数能够     (1)把符合数学格式的数字型字符串转换成整数     (2)把浮点数转换成整数,但是只是简单的取整,而非四舍五入. 举例: 1 aa = int("124&quo ...

  10. Python学习笔记:第3天 字符串的操作

    目录 1. python的数据类型 2. int类型的操作 3. bool类型 4. 字符串的操作 5. for循环 1. python的数据类型 int 整数 str 字符串.一般不会用字符串保存大 ...

随机推荐

  1. python之排序(sort/sorted)

    大家都知道,python排序有内置的排序函数 sort() 和 高阶函数sorted() .但是它们有什么区别呢? 让我们先从这个函数的定义说起: sorted():该函数第一个参数iterable为 ...

  2. vue-resource 全局拦截器

    项目中可能会添加超时登录的功能,因此根据tokenid 判断是否超时.如果token已过期,需要跳转至登录页面. 因此需要用到全局拦截器拦截返回的状态 //下边代码添加在main.js中 Vue.ht ...

  3. slf4j日志的使用-学习笔记

    maven项目: 一.首先在pom.xml文件中添加maven依赖 这是其中一种: <dependency>     <groupId>org.slf4j</groupI ...

  4. 惟一ID生成方法

    几乎所有的业务系统,都存在生成惟一ID的需求,例如: 用户ID:user_id 订单ID: order_id 消息ID: msg_id 常见的ID生成有三大类方法: 一.中间件实现 1.利用Mysql ...

  5. java在遍历列表的时候删除列表中某个元素

    在遍历list的时候需要删除其中的某些元素,不要用foreach遍历,需要用Iterator. List<String> list = new ArrayList<String> ...

  6. (转)Java并发编程:核心理论

    原文链接:https://www.cnblogs.com/paddix/p/5374810.html Java并发编程系列: Java 并发编程:核心理论 Java并发编程:Synchronized及 ...

  7. 六,k8s集群service资源

    目录 Service简介 ClusterIP Headless(无头service) NodePort Service简介 service的基本说明: Service 是作用于客户端可服务端(Pod) ...

  8. Werkzeug(Flask)之Local、LocalStack和LocalProxy

  9. 【CF1187E】Tree Painting

    题目大意:给定一棵 N 个点的树,初始全是白点.要求你做 N 步操作,每一次选定一个与一个黑点相隔一条边的白点,将它染成黑点,然后获得该白点被染色前所在的白色联通块大小的权值.第一次操作可以任意选点, ...

  10. 组件(2):使用Prop下发数据

    数据下发 组件实例的作用域是相互独立的,父.子组件之间无法进行数据的共享.如果想在子组件模板中使用父组件的数据,可以通过Prop将父组件的数据下发到子组件.子组件用props选项声明它预期的数据. 为 ...