读写文本文件 [英] Reading and writing a text file

查看:51
本文介绍了读写文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在读取或写入数据到文件后,是否有必要在Python中关闭文件?。在参考Python材料时,我看到一些提到不需要的地方关闭文件。如果我错了,请更正我。


如果可能,任何人都可以帮我提供示例代码来读写一个简单的文本文件。我看到有很多方法可以在Python中读/写数据。但是我想使用有效的方式从文件中读取或写入数据。


提前谢谢

PSB

Hi,

Is it necessary in Python to close the File after reading or writing the data to file?.While refering to Python material ,I saw some where mentioning that no need to close the file.Correct me if I am wrong.

If possible could anybody help me with sample code for reading and writing a simple text file.I have seen there are many ways to read /write the data in Python.But I want to use the effective way of reading or writing the data from and to file.

Thanks in advance
PSB

推荐答案





在Python中是否有必要在读取或写入数据到文件后关闭文件?。在参考Python材料时,我看到一些提到不需要关闭文件的地方。如果我错了,请更正我。


如果可能,任何人都可以帮我阅读和编写一个简单的文本文件的示例代码。我已经看到了有许多方法可以在Python中读/写数据。但我想使用有效的方式来读取或写入数据文件。


提前谢谢

PSB
Hi,

Is it necessary in Python to close the File after reading or writing the data to file?.While refering to Python material ,I saw some where mentioning that no need to close the file.Correct me if I am wrong.

If possible could anybody help me with sample code for reading and writing a simple text file.I have seen there are many ways to read /write the data in Python.But I want to use the effective way of reading or writing the data from and to file.

Thanks in advance
PSB



此线程显示如何读取和写入数据: http://www.thescripts.com/forum/thre...7166-1-10.html

有我参与的文件I / O上的其他几个主题。


当重新分配文件对象引用或减少为无时,Python将在其垃圾收集例程中关闭一个打开的文件。最好关闭每个打开的文件 - 尤其是在创建文件对象时。这引出了一个困扰我的主题。打开这样的文件:

This thread shows how to read and write data:http://www.thescripts.com/forum/thre...7166-1-10.html
There are several other theads on file I/O that I have participated in.

Python will close an open file in its garbage collection routine when the file object reference is reassigned or decreases to None. It is good practice to close every file that is opened - especially when a file object was created. This brings up a subject that has puzzled me. Open a file like this:

展开 | 选择 | Wrap | 行号



这会引起一个令我困惑的主题。打开这样的文件:
This brings up a subject that has puzzled me. Open a file like this:
展开 | 选择 | Wrap | 行号


我有这种格式的文件数据


员工#员工姓名薪资位置

------------------------------------- -------------------------------------------------- ------

121111 Sam 10,000 NJ

121311 Paul 20,000 NY

111111 Jim 10,000 TX


数据是在Xls中,我们手动复制到文本文件中。复制到文本文件后,数据没有像我们在xls中看到的那样组织。


那么如何读取这个文件数据(不使用切片概念)并存储在相应的领域。


可以任何人提供一段示例代码。


谢谢

PSB
I have a file data in this format

Employee # Employee Name Salary Location
---------------------------------------------------------------------------------------------
121111 Sam 10,000 NJ
121311 Paul 20,000 NY
111111 Jim 10,000 TX

The data is in Xls and we are copying manually into text file.After copying into text file ,the data is not organized as we see in xls.

So how to read this file data (without using slicing concept) and store in the respective fields.

Could anybody provide a sample piece of code.

Thanks
PSB


这篇关于读写文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆