I have a text file that I need to modify. It contains about 100 or so lines formated as follows:

123456789*DOE JOHN A          *PY*SUPER HERO       *179250*026450*000000*68*026450*  *000000*  *000000*  *000000*  *000000

What I need to do is insert decimal points at specific places so it looks like this:

123456789*DOE JOHN A          *PY*SUPER HERO       *1792.50*0264.50*0000.00*68*0264.50*  *0000.00*  *0000.00*  *0000.00*  *0000.00

Can someone point me in the right direction please?

I had to wrap these in code tags to try to preserve the whitespace... it's the only way I could find to do it. :icon_rolleyes:
They're also one line of text, so "Toggle Plain Text" will show closer to what they really are.

Recommended Answers

All 10 Replies

Use rec.split("*") and then convert the strings to an int and divide by float(100). Remember to round if converting back to a string as floats are not exact. Or you can use slice[:-2] to get everything except the last 2 digits and [-2:] to get only the last 2, and then add the decimal point but that would be the long way around.

After you open the file for reading you can iterate over each line like so:

fh = open('myfile.txt', 'r')
lines = fh.readlines()
fh.close()

for each_line in lines:
   ## split each line now like woooee pointed out above and do your processing...

Thanks guys.
I accomplished what I needed to do this way.
I'm sure it's not the best way, but it works for what I needed.
Any comments or ways to improve my coding will be greatly appreciated.

fileName = 'file1.txt'
fileName2 = 'file2.txt'

f = open(fileName, "r")
g = open(fileName2, 'w')
lines = f.readlines()

for line in lines:
    nlist = list(line)
    nlist.insert(96, '.')
    nlist.insert(104, '.')
    nlist.insert(112, '.')
    nlist.insert(123, '.')
    nlist.insert(134, '.')
    nlist.insert(145, '.')
    nlist.insert(156, '.')
    nlist.insert(167, '.')
    ''.join(nlist)
    g.writelines(nlist)
    
f.close()
g.close()

Congratulations, looks pretty clever to me!

Here is my solution. Since you're learning python, your job is to understand the details :).

import re

digits_pat = re.compile(r"\d{3}(?:[*]|$)")

def digits_trans(mo):
	s = mo.group(0)
	return "%s.%s" % (s[0], s[1:])

def digits_sub(theString):
	return digits_pat.sub(digits_trans, theString)

def addDots(fileIn, fileOut, startAt=20):
	fout = open(fileOut, "w")
	for line in open(fileIn, "r"):
		line = line[:startAt] + digits_sub(line[startAt:])
		fout.write(line)
	fout.close()

if __name__ == "__main__":
	addDots("file1.txt", "file2.txt")

Thanks sneekula.

Gribouillis, you got me with that one. I'm definately going to do some research on this.
One problem, it doesn't insert the decimal point into the last set of numbers.
If you would be so kind as to jump start me in the right direction, what would you change to fix that? :icon_smile:

I'd like to help you, but you must tell me where are the missing decimal points. I tried with the data at the top of the thread and it works ...

I'd like to help you, but you must tell me where are the missing decimal points. I tried with the data at the top of the thread and it works ...

All 100 or so lines look like this after running it:

123456789*DOE JOHN A *PY*SUPER HERO *1792.50*0264.50*0000.00*68*0264.50* *0000.00* *0000.00* *0000.00* *000000

Gribouillis,

I think I may have figured out what the deal is when I run your program compared to when you run it.
My text file (file1.txt) is 400 characters wide. (It's automatically created, so that's just the way it is)
The example I posted above doesn't have all the extra whitespace after the last set of digits.

Then a fix could be

digits_pat = re.compile(r"\d{3}(?:[*\s]|$)")
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.