The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by cliffalanmartin]

I've got a problem that seems perfect for Python to handle. I have a data file
that I typically run through a program I've written in Mathematica. This file
is a ; delimited ascii file. Sometimes the colons end up at the beginning of
a line with an newline and Mathematica methods choke. I can change the file by
hand but it seemed a chance to try to learn something about Python. I can find
the number of lines in a Python script but if I try to use tell() it gives me
the same number for each occurence of the file. What I want to do is simple.
Find the occurence of each ; at the beginning of a line and a newline. Move
the ; to the end of the preceeding line and eliminated the newline. Sorry for
the slight incoherence but Python seems to be full of methods with too many
restrictions on what I can do. Thanks in advance.
Cliff
import-bot (20211) [Avatar] Offline
#2
Re: Finding and changing a character
[Originally posted by daryl harms]

> I've got a problem that seems perfect for Python to handle. I have a data file
> that I typically run through a program I've written in Mathematica. This file
> is a ; delimited ascii file. Sometimes the colons end up at the beginning of
> a line with an newline and Mathematica methods choke. I can change the file by
> hand but it seemed a chance to try to learn something about Python. I can find
> the number of lines in a Python script but if I try to use tell() it gives me
> the same number for each occurence of the file. What I want to do is simple.
> Find the occurence of each ; at the beginning of a line and a newline. Move
> the ; to the end of the preceeding line and eliminated the newline. Sorry for
> the slight incoherence but Python seems to be full of methods with too many
> restrictions on what I can do. Thanks in advance.
> Cliff
import-bot (20211) [Avatar] Offline
#3
Re: Finding and changing a character
[Originally posted by daryl harms]

> I've got a problem that seems perfect for Python to handle. I have a data file
> that I typically run through a program I've written in Mathematica. This file
> is a ; delimited ascii file. Sometimes the colons end up at the beginning of
> a line with an newline and Mathematica methods choke. I can change the file by
> hand but it seemed a chance to try to learn something about Python. I can find
> the number of lines in a Python script but if I try to use tell() it gives me
> the same number for each occurence of the file. What I want to do is simple.
> Find the occurence of each ; at the beginning of a line and a newline. Move
> the ; to the end of the preceeding line and eliminated the newline. Sorry for
> the slight incoherence but Python seems to be full of methods with too many
> restrictions on what I can do. Thanks in advance.
> Cliff

Hi Cliff,

The following will hopefully give you some ideas on how you could go about
dealing with this problem:

>>> f = open('origfile','r')
>>> origLines = f.readlines()
>>> fixedLines = []
>>> for line in origLines:
if line.rstrip() == ';':
# fix the previous line but don't add the current line
fixedLines[-1] = fixedLines[-1].rstrip() + ';
'
else:
# add all other lines
fixedLines.append(line)


>>> origLines
['aaa aaa aaa;12', 'bbb bbb bbb;12', 'ccc ccc ccc12', ';12', 'ddd ddd
ddd;12']
>>> fixedLines
['aaa aaa aaa;12', 'bbb bbb bbb;12', 'ccc ccc ccc;12', 'ddd ddd ddd;12']
>>>

This just print out "fixedLines" but you could just write it out as your new
file using the "writelines" method of the file object.

Note that the rstrip method of a string will strip out any trailing spaces and
the newline. That is it performs the same function as string.rstrip which is
used in the book.

Daryl