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.
We have worked with different kind of numbers in Python and modified their type as per our need.
In this tutorial, we will discuss how we can remove decimal in Python.
Let's start with a simple program,
Output:
Explanation:
In the above program, we have declared a,b and c as 24, 19.4, and 3+4j respectively.
On checking their type, we came to know a belongs to class 'int', b belongs to class 'float' and c belongs to class 'complex'.
Here we have to work on the float numbers, so let's list out the different methods of removing decimals from numbers.
- Using trunc[] function
- Using int[]
- Using split[]
Let's discuss each one of them in detail-
Using trunc[] Function
In the first program, we will make use of trunc[] function and remove the decimal present in the numbers.
The following program illustrates the same-
Output:
523 21 182 211 19
Explanation:
Let's have a look at the explanation of the above program-
- Since we have to use the trunc[] function, we have imported the math module.
- We have provided five different decimal values to five variables and checked their type after they are passed in the trunc[] function.
- On executing the program, it displays the required output.
Using int[]
It's time to know the second approach which is removing decimal using int[].
The program given below shows how it can be done-
Output:
523 21 182
Explanation:
Let's understand what we have done here-
- In the first step, we have provided float values to three variables and checked their type.
- After this, we have passed each variable to int[] and stored them to a new variable.
- Finally, we have printed the values stored in these variables.
- On executing this program, the expected output is displayed.
Using split[]
Finally, in the last approach, we will use the interesting split[] to obtain the integer values.
The following program illustrates the same-
Output:
The resultant list is: [523, 21, 182, 211, 19]
Explanation:
Let's have a look at the explanation of the above program-
- In the first step, we have created a list that contains all the decimal values.
- After this, we have declared an empty list and appended the values in it.
- In the next step, we have taken each element from that list and passed it into an int[].
- Finally, we have displayed the resultant list that contains the numbers without decimal.
Conclusion
In this Tutorial, we started with a general idea of the type of numbers we use in Python and then learned the various methods of removing decimals from the numbers.
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
In python programming, sometimes it is necessary to remove all decimals from a number to get the required output. These decimals are also called Floating point numbers in Python. Basically, there are 3 numerical data types in python. They are integers[int[]], floating-point numbers[float[]] and complex[complex[]] data types. Type conversion in python helps to convert a decimal value number[floating number] to an integer. Thus converting float->int removes all decimals from a number.
There are three methods for removing all decimals from a number using python
Methods:
- Using int[ ] function
- Using trunc[ ]function
- Using split[ ] function
Method 1: Using int[ ] [Type conversion] :
int[ ] is a built-in function used to convert any value into an integer number.
Python3
Number1
=
44.560
Number2
=
856.9785623
Number3
=
9999.99
New_Number1
=
int
[Number1]
New_Number2
=
int
[Number2]
New_Number3
=
int
[Number3]
print
[
"Number1 = "
, New_Number1]
print
[
"Number2 = "
, New_Number2]
print
[
"Number3 = "
, New_Number3]
print
[
type
[Number1]]
print
[
type
[New_Number1]]
Output :
Number1 = 44 Number2 = 856 Number3 = 9999
Method 2: Using truncate function[trunc[ ]] :
The math[ ] module is a standard built-in module in python. There are numerous mathematical functions defined in math[ ] module. To use the truncate function, first, the math module must be imported, using trunc[ ] function without defining the math[ ] module gives an error. By using math.trunc[ ] function a number can be truncated in python.
Python3
import
math
num1
=
math.trunc[
450.63
]
print
[num1]
print
[math.trunc[
999998.99999
]]
print
[math.trunc[
-
89.99
]]
print
[math.trunc[
0.99
]]
Output :
450 999998 -89 0
Method 3 : Using split[ ] function
The split[ ] function only works on strings. Hence, the decimal values are converted to a string using str[ ] function and then split at the decimal point. The values remain in the string data type after performing split[ ] function. Therefore, the values are again converted to the integer data type.
Python3
value1
=
[
998.99
,
56.8
,
25.6
,
-
52.0
]
lst
=
[]
for
each
in
value1:
lst.append[
str
[each].split[
'.'
][
0
]]
final_list
=
[
int
[i]
for
i
in
lst]
print
[final_list]
Output:
[998, 56, 25, -52]
Note: Using of int[] function for removing all the decimal values is easy and saves time with just a single line of code.