hadoop学习之hive
2015-11-21 00:15
399 查看
1.文件读取的三种方法
方法一:
代码如下:
f = open("foo.txt") # 返回一个文件对象
line = f.readline() # 调用文件的 readline()方法
while line:
print line, # 后面跟 ',' 将忽略换行符
# print(line, end = '') # 在 Python 3中使用
line = f.readline()
f.close()
方法二:
复制代码 代码如下:
for line in open("foo.txt"):
print line,
方法三:
复制代码 代码如下:
f = open("c:\\1.txt","r")
lines = f.readlines()#读取全部内容
for line in lines
print line
wirte()方法把字符串写入文件,writelines()方法可以把列表中存储的内容写入文件。
f=file("hello.txt","w+")
li=["hello world\n","hello china\n"]
f.writelines(li)
f.close()
文件的内容:
hello world
hello china
write()和writelines()这两个方法在写入前会清除文件中原有的内容,再重新写入新的内容,相当于“覆盖”的方法。如果需要保留文件中原有的内容,只是需要追加新的内容,可以使用“a+”模式
打开文件。
f=file("hello.txt","a+")
new_context="goodbye"
f.write(new_content)
f.close()
此时hello.txt中的内容如下所示:
hello world
hello china
goodbye
实践:
>>> f=file("hello.txt","w+")
>>> li=["hello world\n","hello china\n"]
>>> f.writelines(li)
>>> f.close()
>>>
>>> f=file("hello.txt","a+")
>>> new_context="goodbye"
>>> f.write(new_content)
>>> f.write(new_content)
>>> f.close()
结果:
hello world
hello china
goodbyegoodbye
方法一:
代码如下:
f = open("foo.txt") # 返回一个文件对象
line = f.readline() # 调用文件的 readline()方法
while line:
print line, # 后面跟 ',' 将忽略换行符
# print(line, end = '') # 在 Python 3中使用
line = f.readline()
f.close()
方法二:
复制代码 代码如下:
for line in open("foo.txt"):
print line,
方法三:
复制代码 代码如下:
f = open("c:\\1.txt","r")
lines = f.readlines()#读取全部内容
for line in lines
print line
wirte()方法把字符串写入文件,writelines()方法可以把列表中存储的内容写入文件。
f=file("hello.txt","w+")
li=["hello world\n","hello china\n"]
f.writelines(li)
f.close()
文件的内容:
hello world
hello china
write()和writelines()这两个方法在写入前会清除文件中原有的内容,再重新写入新的内容,相当于“覆盖”的方法。如果需要保留文件中原有的内容,只是需要追加新的内容,可以使用“a+”模式
打开文件。
f=file("hello.txt","a+")
new_context="goodbye"
f.write(new_content)
f.close()
此时hello.txt中的内容如下所示:
hello world
hello china
goodbye
实践:
>>> f=file("hello.txt","w+")
>>> li=["hello world\n","hello china\n"]
>>> f.writelines(li)
>>> f.close()
>>>
>>> f=file("hello.txt","a+")
>>> new_context="goodbye"
>>> f.write(new_content)
>>> f.write(new_content)
>>> f.close()
结果:
hello world
hello china
goodbyegoodbye
相关文章推荐
- MVC与三层架构区别
- 浅析三层架构与MVC模式的区别
- 服务器架构图
- 解决ecshop订单管理退货时,如何做到不退邮费
- 我从其他Shell脚本中学到了什么?
- mysql5.6 rpm模式centos安装纪要
- top
- 生产服务器环境最小化安装后Centos 6.5优化配置备忘
- [Django架构流程分析]请求处理机制其三:view层与模板解析
- [Kali Linux 2.0] DSL配置问题
- 《coredump问题原理探究》Linux x86版7.10节set相关的iterator对象
- Linux教程【一】
- Nginx日志分析脚本
- openssl windows编译 32位&64位
- tiny6410移植opencv
- POJ 3308 Paratroopers 最小点权覆盖Dinic
- 使用mutt命令发送邮件(Centos/RHEL) For Gmail
- [Django架构流程分析]请求处理机制其二:Django中间件的解析
- Apache服务器的vhost------可实现在同一台服务器上放置多个网站
- OpenGL中的坐标系