473,739 Members | 6,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem parsing lines in a file

I'm having difficulty getting the following code to work. All I want
to do is remove the '0:00:00' from the end of each line. Here is part
of the original file:

3,3,"Dyspepsia NOS",9/12/2003 0:00:00
4,3,"OA of lower leg",9/12/2003 0:00:00
5,4,"Cholera NOS",9/12/2003 0:00:00
6,4,"Open wound of ear NEC*",9/12/2003 0:00:00
7,4,"Migraine with aura",9/12/2003 0:00:00
8,6,"HTN [Hypertension]",10/15/2003 0:00:00
10,3,"Imerslund syndrome",10/27/2003 0:00:00
12,4,"Juvenile neurosyphilis", 11/4/2003 0:00:00
13,4,"Benign paroxysmal positional nystagmus",11/4/2003 0:00:00
14,3,"Salmonell a infection, unspecified",11/7/2003 0:00:00
20,3,"Bubonic plague",11/11/2003 0:00:00

output = open('my/path/ProblemListFixe d.txt', 'w')
for line in open('my/path/ProblemList.txt ', 'r'):
newline = line.rstrip('0: 00:00')
output.write(ne wline)
output.close()
This result is a copy of "ProblemLis t" without any changes made. What
am I doing wrong? Thanks for any help.

Mike
Dec 11 '07 #1
5 1405
barronmo wrote:
I'm having difficulty getting the following code to work. All I want
to do is remove the '0:00:00' from the end of each line. Here is part
of the original file:

3,3,"Dyspepsia NOS",9/12/2003 0:00:00
4,3,"OA of lower leg",9/12/2003 0:00:00
5,4,"Cholera NOS",9/12/2003 0:00:00
6,4,"Open wound of ear NEC*",9/12/2003 0:00:00
7,4,"Migraine with aura",9/12/2003 0:00:00
8,6,"HTN [Hypertension]",10/15/2003 0:00:00
10,3,"Imerslund syndrome",10/27/2003 0:00:00
12,4,"Juvenile neurosyphilis", 11/4/2003 0:00:00
13,4,"Benign paroxysmal positional nystagmus",11/4/2003 0:00:00
14,3,"Salmonell a infection, unspecified",11/7/2003 0:00:00
20,3,"Bubonic plague",11/11/2003 0:00:00

output = open('my/path/ProblemListFixe d.txt', 'w')
for line in open('my/path/ProblemList.txt ', 'r'):
newline = line.rstrip('0: 00:00')
output.write(ne wline)
output.close()
This result is a copy of "ProblemLis t" without any changes made. What
am I doing wrong? Thanks for any help.
It works kind of for me - but it's actually a bit more than you want. Take a
close look on what rstrip _really_ does. Small hint:

print "foobar".rstrip ("rab")

Diez
Dec 11 '07 #2
On Dec 11, 7:25 pm, barronmo <barro...@gmail .comwrote:
I'm having difficulty getting the following code to work. All I want
to do is remove the '0:00:00' from the end of each line. Here is part
of the original file:

3,3,"Dyspepsia NOS",9/12/2003 0:00:00
4,3,"OA of lower leg",9/12/2003 0:00:00
5,4,"Cholera NOS",9/12/2003 0:00:00
6,4,"Open wound of ear NEC*",9/12/2003 0:00:00
7,4,"Migraine with aura",9/12/2003 0:00:00
8,6,"HTN [Hypertension]",10/15/2003 0:00:00
10,3,"Imerslund syndrome",10/27/2003 0:00:00
12,4,"Juvenile neurosyphilis", 11/4/2003 0:00:00
13,4,"Benign paroxysmal positional nystagmus",11/4/2003 0:00:00
14,3,"Salmonell a infection, unspecified",11/7/2003 0:00:00
20,3,"Bubonic plague",11/11/2003 0:00:00

output = open('my/path/ProblemListFixe d.txt', 'w')
for line in open('my/path/ProblemList.txt ', 'r'):
newline = line.rstrip('0: 00:00')
output.write(ne wline)
output.close()

This result is a copy of "ProblemLis t" without any changes made. What
am I doing wrong? Thanks for any help.

Mike
rstrip() won't do what you think it should do.
you could either use .replace('0:00: 00','') directly on the input
string or if it might occur in one of the other elements as well then
just split the line on delimeters.
In the first case you can do.

for line in input_file:
output_file.wri te( line.replace('0 :00:00','') )

in the latter rather.

for line in input_file:
tmp = line.split( ',' )
tmp[3] = tmp[3].replace('0:00: 00')
output_file.wri te( ','.join( tmp ) )

Hope that helps,
Chris
Dec 11 '07 #3
This result is a copy of "ProblemLis t" without any changes made. What
am I doing wrong? Thanks for any help.
rstrip doesn't work the way you think it does
>>help(str.rstr ip)
Help on method_descript or:

rstrip(...)
S.rstrip([chars]) -string or unicode

Return a copy of the string S with trailing whitespace removed.
If chars is given and not None, remove characters in chars
instead.
If chars is unicode, S will be converted to unicode before
stripping
>>'abcdef'.rstr ip('e')
'abcdef'
>>'abcdef'.rstr ip('ef')
'abcd'
>>'abcdef'.rstr ip('efc')
'abcd'
>>'abcdef'.rstr ip('efcd')
'ab'
>>'20,3,"Buboni c plague",11/11/2003 0:00:00\n'.rstr ip('0:00:00')
'20,3,"Bubonic plague",11/11/2003 0:00:00\n'
>>'20,3,"Buboni c plague",11/11/2003 0:00:00\n'.rstr ip('0:00:00\n')
'20,3,"Bubonic plague",11/11/2003 '
>>'20,3,"Buboni c plague",11/11/2003 0:00:00\n'.rstr ip('0:\n')
'20,3,"Bubonic plague",11/11/2003 '

You probably just want to use slicing though:
>>'20,3,"Buboni c plague",11/11/2003 0:00:00\n'[:-9]
'20,3,"Bubonic plague",11/11/2003'

But don't forget to re-attach a newline before writing out. That goes
for the first method also.

Matt
Dec 11 '07 #4
barronmo wrote:
I'm having difficulty getting the following code to work. All I want
to do is remove the '0:00:00' from the end of each line. Here is part
of the original file:

3,3,"Dyspepsia NOS",9/12/2003 0:00:00
...
20,3,"Bubonic plague",11/11/2003 0:00:00

output = open('my/path/ProblemListFixe d.txt', 'w')
for line in open('my/path/ProblemList.txt ', 'r'):
newline = line.rstrip('0: 00:00')
output.write(ne wline)
output.close()
This result is a copy of "ProblemLis t" without any changes made. What
am I doing wrong? Thanks for any help.
You should feel lucky that it didn't work :-) If you would have used
newline = line.rstrip('0: 00:00\n')
you would not have found the problem, nor the solution.
--
Kees

Dec 12 '07 #5
Thanks everyone. I learned several things on this one. I ended up
using the .replace() method and got the results I wanted.

Thanks again,

Michael Barron
Dec 13 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4754
by: Rami A. Kishek | last post by:
Hi - this mysterious behavior with shelve is just about to kill me. I hope someone here can shed some light. First of all, I have this piece of code which uses shelve to save instances of some class I define. It works perfectly on an old machine (PII-400) running Python 2.2.1 under RedHat Linux 8.0. When I try to run it under Python for windows ME on a P-4 1.4 GHz, however, it keeps crashing on reading from the shelved file the second...
11
3572
by: Johnny Lee | last post by:
Hi, I was using urllib to grab urls from web. here is the work flow of my program: 1. Get base url and max number of urls from user 2. Call filter to validate the base url 3. Read the source of the base url and grab all the urls from "href" property of "a" tag 4. Call filter to validate every url grabbed 5. Continue 3-4 until the number of url grabbed gets the limit
1
2297
by: Thomas Kowalski | last post by:
Hi, I have to parse a plain, ascii text file (on local HD). Since the file might be many millions lines long I want to improve the efficiency of my parsing process. The resulting data structure shall look like this the following: class A { ... int value; }
22
1912
by: sam_cit | last post by:
Hi Everyone, I have the following structure in my program struct sample { char *string; int string_len; };
9
1989
by: Paulers | last post by:
Hello, I have a log file that contains many multi-line messages. What is the best approach to take for extracting data out of each message and populating object properties to be stored in an ArrayList? I have tried looping through the logfile using regex, if statements and flags to find the start and end of each message but I do not see a good time in this process to create a new instance of my Message object. While messing around with...
1
1765
by: Robert Neville | last post by:
Basically, I want to create a table in html, xml, or xslt; with any number of regular expressions; a script (Perl or Python) which reads each table row (regex and replacement); and performs the replacement on any file name, folder, or text file (e.g. css, php, html). For example, I often rename my mp3 (files); the folder holding the mp3 files; and replace these renamed values in a playlist/m3u/xml file. The table should hold clean...
12
3402
by: Julian | last post by:
Hi, I am having problems with a function that I have been using in my program to read sentences from a 'command file' and parse them into commands. the surprising thing is that the program works fine on some computers and not so fine on others. I tried debugging and cannot make any sense of it. I narrowed it down to the seekg function and made this simple program which (from what I understand) does not seem to be working as expected in all...
13
4511
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple command set consisting of several single letter commands which take no arguments. A few additional single letter commands take arguments:
2
2589
by: python | last post by:
I'm parsing a text file for a proprietary product that has the following 2 directives: #include <somefile> #define <name<value> Defined constants are referenced via <#name#syntax. I'm looking for a single text stream that results from processing a file containing these directives. Even better would be an iterator(?) type
22
3826
by: V S Rawat | last post by:
(bringing the discussion here for php.general) I am on xpsp3, wampserver 2.0, having apache 2.2.8, php 5.2.6, MySQL 5.0.51b. http://localhost/ is E:\wamp\www\ I have put the first php script to hello.php file: <html> <head> <title>PHP Test</title>
0
8969
marktang
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...
0
9479
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9266
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,...
1
6754
isladogs
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...
0
6054
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();...
0
4826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3280
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
2
2748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.