01ython内部数据获取_读写txt

Python本地读写:txt文件操作

txt是一种很常见的文件格式。它是由字符串行组成,每行由EOL(end of line)字符隔开:“\n”

要打开文件可以用open(path/filename, access_mode)实现,access_mode有两种模式:一种是“读”,一种是“写”,分别用”r“,”w“表示。

在读取完文件之后,记得一定一定一定要关闭文件:.close()。
否则会一直保持着与文件的连接,消耗资源。

1. 读操作

1.1 读取全部内容

读取全部内容直接调用.read()即可

运行以下程序,会将指定的文件读入,打印结果与打开txt文件的内容格式是一样的,每一行是一个字符串。

但是,如果直接显示all_content,每行之间就会出现”\r\n”的换行符。

# 一般都会在外部先定义好文件的路径
txt_filename = './files/python_wiki.txt'

# 打开文件
file_obj = open(txt_filename, 'r')

# 读取整个文件内容
all_content = file_obj.read()

# 关闭文件
file_obj.close()

print all_content

all_content
Python is a widely used high-level, general-purpose, interpreted, dynamic programming language.[24][25] Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than possible in languages such as C++ or Java.[26][27] The language provides constructs intended to enable writing clear programs on both a small and large scale.[28]
Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.[29]
Python interpreters are available for many operating systems, allowing Python code to run on a wide variety of systems. CPython, the reference implementation of Python, is open source software[30] and has a community-based development model, as do nearly all of its variant implementations. CPython is managed by the non-profit Python Software Foundation.





'Python is a widely used high-level, general-purpose, interpreted, dynamic programming language.[24][25] Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than possible in languages such as C++ or Java.[26][27] The language provides constructs intended to enable writing clear programs on both a small and large scale.[28]\r\nPython supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.[29]\r\nPython interpreters are available for many operating systems, allowing Python code to run on a wide variety of systems. CPython, the reference implementation of Python, is open source software[30] and has a community-based development model, as do nearly all of its variant implementations. CPython is managed by the non-profit Python Software Foundation.'

1.2 逐行读取

有的时候我们会需要先读几行进来看看数据的格式是咋样的,并不需要全部将很大的文件读进来。

使用.readline()方法就可以每次只读一行。

第一次运行的时候会读进第一行,当第二次运行的时候,就会自动读第二行,以此类推。

以下例子中,第一次调用了.readline()并赋值给line1,打印出来是第一行;当第二次运行.readline()并赋值line2,打印出来是第二行内容。

txt_filename = './files/python_wiki.txt'

# 打开文件
file_obj = open(txt_filename, 'r')

# 逐行读取
line1 = file_obj.readline()
print line1
Python is a widely used high-level, general-purpose, interpreted, dynamic programming language.[24][25] Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than possible in languages such as C++ or Java.[26][27] The language provides constructs intended to enable writing clear programs on both a small and large scale.[28]
# 继续读下一行
line2 = file_obj.readline()
print line2

# 关闭文件
file_obj.close()
Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.[29]

如果要逐行打印的话,可以直接设置for循环。

txt_filename = './files/python_wiki.txt'

# 打开文件
file_obj = open(txt_filename, 'r')

for i in range(3):
    print file_obj.readline()

file_obj.close()
Python is a widely used high-level, general-purpose, interpreted, dynamic programming language.[24][25] Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than possible in languages such as C++ or Java.[26][27] The language provides constructs intended to enable writing clear programs on both a small and large scale.[28]

Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.[29]

Python interpreters are available for many operating systems, allowing Python code to run on a wide variety of systems. CPython, the reference implementation of Python, is open source software[30] and has a community-based development model, as do nearly all of its variant implementations. CPython is managed by the non-profit Python Software Foundation.

1.3 读取全部内容,返回列表

调用.readlines()可以将文件中的全部内容都读出来,并且返回的格式的“列表”格式

txt_filename = './files/python_wiki.txt'

# 打开文件
file_obj = open(txt_filename, 'r')

lines = file_obj.readlines()

for i, line in enumerate(lines):
    print '%i: %s' %(i, line)

# 关闭文件
file_obj.close()
0: Python is a widely used high-level, general-purpose, interpreted, dynamic programming language.[24][25] Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than possible in languages such as C++ or Java.[26][27] The language provides constructs intended to enable writing clear programs on both a small and large scale.[28]

1: Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.[29]

2: Python interpreters are available for many operating systems, allowing Python code to run on a wide variety of systems. CPython, the reference implementation of Python, is open source software[30] and has a community-based development model, as do nearly all of its variant implementations. CPython is managed by the non-profit Python Software Foundation.

写操作

写操作的模式是”w”

要写入文件的时候调用.write()方法

txt_filename = './files/test_write.txt'

# 打开文件
file_obj = open(txt_filename, 'w')

# 写入全部内容
file_obj.write("《Python数据分析》")
file_obj.close()
txt_filename = './files/test_write.txt'

# 打开文件
file_obj = open(txt_filename, 'w')

# 写入字符串列表
lines = ['这是第%i行\n' %i for i in range(100)]
file_obj.writelines(lines)
file_obj.close()

with语句

with语句包括了异常处理,自动调用文件关闭操作,非常推荐使用!

每次操作完都要.close关闭以下很麻烦又经常会忘记。所以可以直接用with语句,这样操作完之后就会自动close.

txt_filename = './files/test_write.txt'
with open(txt_filename, 'r') as f_obj:
    print f_obj.read()
这是第0行
这是第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行
这是第26行
这是第27行
这是第28行
这是第29行
这是第30行
这是第31行
这是第32行
这是第33行
这是第34行
这是第35行
这是第36行
这是第37行
这是第38行
这是第39行
这是第40行
这是第41行
这是第42行
这是第43行
这是第44行
这是第45行
这是第46行
这是第47行
这是第48行
这是第49行
这是第50行
这是第51行
这是第52行
这是第53行
这是第54行
这是第55行
这是第56行
这是第57行
这是第58行
这是第59行
这是第60行
这是第61行
这是第62行
这是第63行
这是第64行
这是第65行
这是第66行
这是第67行
这是第68行
这是第69行
这是第70行
这是第71行
这是第72行
这是第73行
这是第74行
这是第75行
这是第76行
这是第77行
这是第78行
这是第79行
这是第80行
这是第81行
这是第82行
这是第83行
这是第84行
这是第85行
这是第86行
这是第87行
这是第88行
这是第89行
这是第90行
这是第91行
这是第92行
这是第93行
这是第94行
这是第95行
这是第96行
这是第97行
这是第98行
这是第99行

注:部分例子来自于小象学院Robin课程

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值