Edit: The title could have been more descriptive, I am sorry for that!

Hello!

First time poster, frequent reader.

I am very green to both programming and python, trying my best and this board has been helpful just by reading.

Now, I have a problem of my own that I want to post.

I got a rather large .txt file (I guess?) and I have already replaced certain words
with the word "value".

Now I want to replace all the "value"'s with numbers starting from 0 .. n.

So far I got this, and it works, however it takes forever (well a few minutes but hey, that is forever!)

# -*- coding: cp1252 -*-

f = open("test.txt")
contents = f.read()
f.close()

print "Antal förekomster av value:", contents.count("value")
wordcount = contents.count("value")

file = open('test.txt')
data = file.read()

count = 0
while (count < wordcount):
    string = str(count)
    data = data.replace('value', string , 1)
    file = open('test.txt', 'w')
    file.write(data)
    file.close()
    count = count + 1
   
print "du är klar"

The question, how can I (if possible) replace all "value" with numbers in a quicker way then above?

Thanks for any help, tips or advice!

Member Avatar for Enalicho

Well -
the biggest time consumers in this piece of code is the read/write.
You open a file, read the contents TWICE before the loop.
While in the loop, you open a file, write the whole file again wordcount times, then close it and begin the loop again.

How about -

Open a file for reading
Read and storing the content
Close the file

Replace [b]all[/b] the words you want to replace and store data

Open a file for closing
Write the new file with the edited data
Close the file

There, now there's only one read operation, and one write operation. That will save you a _lot_ of time.

The question, how can I (if possible) replace all "value" with numbers in a quicker way then above?

Yes you dont need to store data from file,just iterate over the file and do replace.
And use with open() ,then you dont need to close fileobject.
This create a new file,this is ofen the best way so you dont mess up orginal file.

with open('val.txt') as f:
    for line in f:
        with open('new.txt', 'a') as f_out:
            f_out.write(line.replace('value', '1111'))

Well -
the biggest time consumers in this piece of code is the read/write.
You open a file, read the contents TWICE before the loop.
While in the loop, you open a file, write the whole file again wordcount times, then close it and begin the loop again.

How about -

Open a file for reading
Read and storing the content
Close the file

Replace [b]all[/b] the words you want to replace and store data

Open a file for closing
Write the new file with the edited data
Close the file

There, now there's only one read operation, and one write operation. That will save you a _lot_ of time.

I suspected I probably open the file one time too many, and I tried to remove and move around but to no avail.

I think I understand what you are saying, however, I still need the loop I guess? I want the first "value" to be 0, the next 1, next 2 and so on. So I took out the part where I write to the file from the loop, but that did not work.

You got me thinking in a more correct way! I'll keep trying =)

Yes you dont need to store data from file,just iterate over the file and do replace.
And use with open() ,then you dont need to close fileobject.
This create a new file,this is ofen the best way so you dont mess up orginal file.

with open('val.txt') as f:
    for line in f:
        with open('new.txt', 'a') as f_out:
            f_out.write(line.replace('value', '1111'))

This worked. But I want to not just replace it with '1111'. First time 0, then 1, and 2 and so on.

I did this

count = 0
with open('test.txt') as f:
    for line in f:
        with open('new.txt', 'a') as f_out:
            n = str(count)
            f_out.write(line.replace("value", n, 1))
            count = count + 1

print "Klart!"

Then I noticed that it only replaced first 'value' in that line, and moved on. If i take the " , 1 " away from the replace line, it makes the whole line 0, then 1, and so on.

That is why I had my original loop, I replaced the first match of "value" with X, then add 1 + X and replace next match of "value" and so on.

Its a neat code tho, and it introduced me to "with open", cheers =)! Really shows how little it takes sometimes =)

file = open("test.txt", "r")
contents = file.read()
file.close()

print "Antal förekomster av value:", contents.count("value")
wordcount = contents.count("value")


count = 0
while (count < wordcount):
    string = str(count)
    contents = contents.replace('value', string , 1)
    count = count + 1
    print "Bytt ut " + string + " antal value av totalt " + str(wordcount)

file = open('new.txt', 'w')
file.write(contents)
file.close()
      
print "du är klar"

This is my new code.. or, rearranged is more correct. It took about 4 minutes to replace 31160 word with value.

Is that as fast it is gonna get?

This is my correction för dig for previous version you posted:

count = 0
# in later versions (2.72, 3.21) you can write:
#with open('test.txt') as f, open('new.txt', 'w') as f_out:
with open('test.txt') as f:
    with open('new.txt', 'w') as f_out:
        for line in f:
            while 'value' in line:
                count += 1
                line = line.replace("value", str(count), 1)
            f_out.write(line)

print("Klart!")
commented: Great answer. +0

This is my correction för dig for previous version you posted:

count = 0
# in later versions (2.72, 3.21) you can write:
#with open('test.txt') as f, open('new.txt', 'w') as f_out:
with open('test.txt') as f:
    with open('new.txt', 'w') as f_out:
        for line in f:
            while 'value' in line:
                count += 1
                line = line.replace("value", str(count), 1)
            f_out.write(line)

print("Klart!")

lol damn that worked instantly!

Trying to understand what you did, think I got all of it. Will need to do some reading.

Thank you for taking time! All of you!

Can I give you all points or how does that work?

Can I give you all points or how does that work?

You can upvote(downvote) anywhere in Daniweb for any good(bad) posts, not only in your thread. If you want to offer special thanks (or complaints) you can give positive (or negative) reputation according to your status as member, you can influence reputation + or - 1 point at the moment, my positive rep counts as 13 points, negative -3 points, for example. That is in addition of down/upvoting.

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.