3

I have a text file with a string like 0.0.1 and I want to remove the decimals to make it 001 or just 1. So for example a 1.0.1 would become a 101.

I thought about using the round() but it would turn 0.0.1 into 0 and that's not what I want.

3
  • please post your text file Commented Sep 29, 2015 at 17:31
  • the text file would like # 0.0.1 Commented Sep 29, 2015 at 17:32
  • You can use the built in replace() function to replace all of the periods with an empty string. The implementation is new = old.replace('.','') Commented Sep 29, 2015 at 17:46

3 Answers 3

10

You could just remove the '.' between the digits:

s = '0.0.1'
s = s.replace('.', '')

after that you can make it an int:

int(s)

By making it an integer, you will also remove any leading zeros. If you need a string afterwards just convert it back to string:

s = str(int(s))
Sign up to request clarification or add additional context in comments.

Comments

2

You could use join and a comprehension:

>>> s = '0.0.1'
>>> ''.join(c for c in s if c != '.')
'001'

If you want to strip the leading 0s:

>>> str(int(''.join(c for c in s if c != '.')))
'1'

Comments

2

Use replace()

Try something like this code block:

new_file = open('newfile.txt','w')

line_file = open('myfile.txt','r').readlines()

for line_in in line_file:
    line_out = line_in.replace('.','')
    new_file.write(line_out)

That should read your file, remove all the periods, and write the result to a new file.

If it doesn't work for your specific case, comment on this answer and I'll update the codeblock to do what you need.

p.s. As per the comment below, you could make this happen in one line with:

open('newfile.txt','w').write(open('myfile.txt','r').read().replace('.',''))

So use that if you want to.

4 Comments

There's certainly a way to do it that way, though I prefer readlines() as it makes the file easier to iterate through as a list of strings. As part of the list making process, it does strip the newline character. it could be done as new_file.write(open('myfile.txt'.'r').read().replace(',','')) but that isn't readable for someone new to the concept.
no, readlines does not strinp newlines, you will be adding blank lines adding another, why is calling readlines easier than iterating over a file object. with open("myfile.txt") as f:for line in f:..., using read or readlines reads the whole file into memory which is not always possible
Iterating isn't easier to do, it's simply easier for someone to understand. I'm trying to show the OP how it works, rather than simply provide code. I have appended a way to do the whole thing the way I think is the best way. But it's one highly structured line with no iteration.
using with to open and iterating over the file object would be the pythonic way to do it, you are still adding blanks lines in between adding another newline in your first example, readlines does not strip.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.