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
42 4846 bvdet 2,851
Recognized Expert Moderator Specialist
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
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: - lineLst = open('file_name').readlines()
Does Python close the file? No file object is created, so the file is closed when the end of file is reached (I think).
This brings up a subject that has puzzled me. Open a file like this: - lineLst = open('file_name').readlines()
Does Python close the file? No file object is created, so the file is closed when the end of file is reached (I think).
yes Python does close the file when the file object gets garbage collected.
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
bvdet 2,851
Recognized Expert Moderator Specialist
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
You can save the Excel worksheet as a text file. The text file will be tab delimited which can be easily parsed. - """
-
Read a tab delimited file
-
"""
-
-
fn = 'your_file'
-
-
f = open(fn, 'r')
-
labelLst = f.readline().strip().split('\t')
-
lineLst = []
-
-
for line in f:
-
if not line.startswith('#'):
-
lineLst.append(line.strip().split('\t'))
-
-
f.close()
-
-
print labelLst
-
print lineLst
Could anybody help me in reading this data.How to seperate the line data and read.
SET 10 = 1101 1106 1107 1108 1109 1110 1111,
1112 1113 1114 1115 1116 1117 1118,
1119 1120 1121 1122 1123 1124 1125
If anybody provide a sample code for the above it will be helpful.
Thanks in advance
PSB
bvdet 2,851
Recognized Expert Moderator Specialist
Could anybody help me in reading this data.How to seperate the line data and read.
SET 10 = 1101 1106 1107 1108 1109 1110 1111,
1112 1113 1114 1115 1116 1117 1118,
1119 1120 1121 1122 1123 1124 1125
If anybody provide a sample code for the above it will be helpful.
Thanks in advance
PSB
- s = 'SET 10 = 1101 1106 1107 1108 1109 1110 1111,\n 1112 1113 1114 1115 1116 1117 1118,\n 1119 1120 1121 1122 1123 1124 1125'
-
sList = s.split('=')
-
label = sList[0].strip()
-
data = sList[1].strip().split(',\n')
-
datastr = ''.join(data)
-
-
print '%s = %s' % (label, datastr)
-
'''
-
>>> SET 10 = 1101 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
-
'''
Thanks for the reply,
This is the input file :
$
$ SET 10
$
$ hjdsahclaladsal kjls
$PTITLE = SET 10 = SET_110
SET 10 = 1101 1106 1107 1108 1109 1110 1111,
1112 1113 1114 1115 1116 1117 1118,
1119 1120 1121 1122 1123 1124 1125
$ END OF SET 110
$
I have to get the SET # 10 and all the integers starting from the 1101 to 1125.How can I read all the integer numbers from the 1101 to 1125.
Is it possible with the solution provided by you?.If possible what is the modifications has to be done to that piece of code
bvdet 2,851
Recognized Expert Moderator Specialist
Thanks for the reply,
This is the input file :
$
$ SET 10
$
$ hjdsahclaladsal kjls
$PTITLE = SET 10 = SET_110
SET 10 = 1101 1106 1107 1108 1109 1110 1111,
1112 1113 1114 1115 1116 1117 1118,
1119 1120 1121 1122 1123 1124 1125
$ END OF SET 110
$
I have to get the SET # 10 and all the integers starting from the 1101 to 1125.How can I read all the integer numbers from the 1101 to 1125.
Is it possible with the solution provided by you?.If possible what is the modifications has to be done to that piece of code
This will give you a list of integers from the data string in my earlier post: - map(int, datastr.split())
I understand the piece of code what you have posted.But how to capture the data in between the Key Words "SET" and "END".My program should be generic enough to read this data in between this key words
SET 10 = 1101 1106 1107 1108 1109 1110 1111,
1112 1113 1114 1115 1116 1117 1118,
1119 1120 1121 1122 1123 1124 1125
$ END OF SET 110
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Kevin T. Ryan |
last post by:
Hi All -
I'm not sure, but I'm wondering if this is a bug, or maybe (more
likely) I'm misunderstanding something...see below:
>>> f = open('testfile', 'w')
>>> f.write('kevin\n')
>>> f.write('dan\n')
>>> f.write('pat\n')
>>> f.close()
|
by: john smith |
last post by:
Hi, I have a file format that is going to contain some parts in ascii, and
some parts with raw binary data. Should I open this file with ios::bin or
no?
For example:
filename: a.bin
number of points = 123
@@@begin data@@@
gibberish follows.....
|
by: Oliver Knoll |
last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode
(that is just writing, right?). How would one reopen it for reading?
I got the following (which works):
FILE *tmpFile = tmpfile();
/* write into tmpFile */
...
|
by: Jeevan |
last post by:
Hi,
I have an array of data (which I am getting from a socket connection).
I am working on a program which acts on this data but the program is
written to work on data from a file (not from an array). I cannot
change anything in the program but can add some features by which I
can convert this array of data into a file. The easiest thing would be
to write the data into a file (in hard disk) and use it. But I will be
working on thousands...
|
by: Need Helps |
last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like:
mdyn"comment~""comment~"\n
Now, I wrote some code to read these records, and it works perfectly for every date I've tried it on, except when the day is 26. I tried saving a record for 6/26/2004 and 7/26/2004 and it read it in as the day, year, and number of...
| |
by: Alex Buell |
last post by:
I have a small text file which consist of the following data:
]]
And the code I've written is as follows:
]]
The trouble is, I can't work out why it goes into an infinite loop
reading the information from the text file!
Can anyone enlighten me as to what I am doing wrong?
|
by: arne.muller |
last post by:
Hello,
I've come across some problems reading strucutres from binary files.
Basically I've some strutures
typedef struct {
int i;
double x;
int n;
double *mz;
|
by: Clive Green |
last post by:
Hello peeps,
I am using PHP 5.2.2 together with MP3_Id (a PEAR module for reading and
writing MP3 tags). I have been using PHP on the command line (Mac OS X
Unix shell, to be precise), and am getting on more or less OK.
So far, I have managed to parse my tab-delimited .txt file properly into
an array, and then use this array to update the tags on a bunch of MP3
audio files. Nice.
|
by: pbj2009 |
last post by:
Hello all:
I'm pretty stumped on this one. I'm not looking for Code, I'm just trying to figure out the best way to start this since I am new to reading and writing from files. I can't figure out what section (main/btnOpen/frmExceptionsLoad) to put the code and all tutorials have either reading from or writing to and I am confused about putting it together along with the calculations of course.
I have to write a program that when I click a...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |