Converting a scientific notation string to an integer
up vote
0
down vote
favorite
I have a question in Python 3.7. I am opening a text file (input.txt) and creating variables from various lines of the text file. For example my text file looks as follows:
Case1
P
1.00E+02
5.
I need to create variables so that:
title = "Case1"
area = "P"
distance = 100
factor = 5
So far this is what I have:
f = open('C:\input.txt',"r")
title = f.readline().strip()
area = f.readline().strip()
distance = f.readline().strip()
factor = f.readline().strip().strip(".")
f.close
print(title)
print(area)
print(distance)
print(factor)
which results in:
Case1
P
1.00E+02
5
How do I get the distance variable to show up as 100 instead of 1.00E+02?
I found the link below thinking it would help, but wasn't able to solve my problem from there. This is a very small segment of my larger program that was simplified, but if it works here it will work for my needs. My program needs to open a generated text file and produce another text file, so the scientific notation number will change between 1.00E-06 and 1.00E+03. The generated text file needs to have these numbers as integers (i.e. between 0.000001 and 1000). Thanks for any help!
Converting number in scientific notation to int
string python-3.x type-conversion integer scientific-notation
add a comment |
up vote
0
down vote
favorite
I have a question in Python 3.7. I am opening a text file (input.txt) and creating variables from various lines of the text file. For example my text file looks as follows:
Case1
P
1.00E+02
5.
I need to create variables so that:
title = "Case1"
area = "P"
distance = 100
factor = 5
So far this is what I have:
f = open('C:\input.txt',"r")
title = f.readline().strip()
area = f.readline().strip()
distance = f.readline().strip()
factor = f.readline().strip().strip(".")
f.close
print(title)
print(area)
print(distance)
print(factor)
which results in:
Case1
P
1.00E+02
5
How do I get the distance variable to show up as 100 instead of 1.00E+02?
I found the link below thinking it would help, but wasn't able to solve my problem from there. This is a very small segment of my larger program that was simplified, but if it works here it will work for my needs. My program needs to open a generated text file and produce another text file, so the scientific notation number will change between 1.00E-06 and 1.00E+03. The generated text file needs to have these numbers as integers (i.e. between 0.000001 and 1000). Thanks for any help!
Converting number in scientific notation to int
string python-3.x type-conversion integer scientific-notation
2
What programming language are you using?
– Dragonthoughts
Nov 19 at 16:46
Cant believe I forgot to add that; I am using python (version 3.7). I have updated the post.
– A.will
Nov 19 at 18:25
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a question in Python 3.7. I am opening a text file (input.txt) and creating variables from various lines of the text file. For example my text file looks as follows:
Case1
P
1.00E+02
5.
I need to create variables so that:
title = "Case1"
area = "P"
distance = 100
factor = 5
So far this is what I have:
f = open('C:\input.txt',"r")
title = f.readline().strip()
area = f.readline().strip()
distance = f.readline().strip()
factor = f.readline().strip().strip(".")
f.close
print(title)
print(area)
print(distance)
print(factor)
which results in:
Case1
P
1.00E+02
5
How do I get the distance variable to show up as 100 instead of 1.00E+02?
I found the link below thinking it would help, but wasn't able to solve my problem from there. This is a very small segment of my larger program that was simplified, but if it works here it will work for my needs. My program needs to open a generated text file and produce another text file, so the scientific notation number will change between 1.00E-06 and 1.00E+03. The generated text file needs to have these numbers as integers (i.e. between 0.000001 and 1000). Thanks for any help!
Converting number in scientific notation to int
string python-3.x type-conversion integer scientific-notation
I have a question in Python 3.7. I am opening a text file (input.txt) and creating variables from various lines of the text file. For example my text file looks as follows:
Case1
P
1.00E+02
5.
I need to create variables so that:
title = "Case1"
area = "P"
distance = 100
factor = 5
So far this is what I have:
f = open('C:\input.txt',"r")
title = f.readline().strip()
area = f.readline().strip()
distance = f.readline().strip()
factor = f.readline().strip().strip(".")
f.close
print(title)
print(area)
print(distance)
print(factor)
which results in:
Case1
P
1.00E+02
5
How do I get the distance variable to show up as 100 instead of 1.00E+02?
I found the link below thinking it would help, but wasn't able to solve my problem from there. This is a very small segment of my larger program that was simplified, but if it works here it will work for my needs. My program needs to open a generated text file and produce another text file, so the scientific notation number will change between 1.00E-06 and 1.00E+03. The generated text file needs to have these numbers as integers (i.e. between 0.000001 and 1000). Thanks for any help!
Converting number in scientific notation to int
string python-3.x type-conversion integer scientific-notation
string python-3.x type-conversion integer scientific-notation
edited Nov 20 at 10:30
Dragonthoughts
1,5194916
1,5194916
asked Nov 19 at 16:44
A.will
32
32
2
What programming language are you using?
– Dragonthoughts
Nov 19 at 16:46
Cant believe I forgot to add that; I am using python (version 3.7). I have updated the post.
– A.will
Nov 19 at 18:25
add a comment |
2
What programming language are you using?
– Dragonthoughts
Nov 19 at 16:46
Cant believe I forgot to add that; I am using python (version 3.7). I have updated the post.
– A.will
Nov 19 at 18:25
2
2
What programming language are you using?
– Dragonthoughts
Nov 19 at 16:46
What programming language are you using?
– Dragonthoughts
Nov 19 at 16:46
Cant believe I forgot to add that; I am using python (version 3.7). I have updated the post.
– A.will
Nov 19 at 18:25
Cant believe I forgot to add that; I am using python (version 3.7). I have updated the post.
– A.will
Nov 19 at 18:25
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
The link you posted actually gives the answer, albeit in a roundabout way.
int() cannot parse strings that don't already represent integers.
But a scientific number is a float.
So, you need to cast it to float first to parse the string.
Try:
print(float(distance))
For numbers with more decimals (e.g your example of 1.00E-06), you can force float notation all the time. Try:
print(format(float(distance), 'f'))
If you only want a certain number of decimals, try:
print(format(float(distance), '.2f'))
(for example)
Thanks that got me what I needed!
– A.will
Nov 19 at 20:14
Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted.
– PhilB
Nov 19 at 20:34
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
The link you posted actually gives the answer, albeit in a roundabout way.
int() cannot parse strings that don't already represent integers.
But a scientific number is a float.
So, you need to cast it to float first to parse the string.
Try:
print(float(distance))
For numbers with more decimals (e.g your example of 1.00E-06), you can force float notation all the time. Try:
print(format(float(distance), 'f'))
If you only want a certain number of decimals, try:
print(format(float(distance), '.2f'))
(for example)
Thanks that got me what I needed!
– A.will
Nov 19 at 20:14
Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted.
– PhilB
Nov 19 at 20:34
add a comment |
up vote
0
down vote
accepted
The link you posted actually gives the answer, albeit in a roundabout way.
int() cannot parse strings that don't already represent integers.
But a scientific number is a float.
So, you need to cast it to float first to parse the string.
Try:
print(float(distance))
For numbers with more decimals (e.g your example of 1.00E-06), you can force float notation all the time. Try:
print(format(float(distance), 'f'))
If you only want a certain number of decimals, try:
print(format(float(distance), '.2f'))
(for example)
Thanks that got me what I needed!
– A.will
Nov 19 at 20:14
Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted.
– PhilB
Nov 19 at 20:34
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
The link you posted actually gives the answer, albeit in a roundabout way.
int() cannot parse strings that don't already represent integers.
But a scientific number is a float.
So, you need to cast it to float first to parse the string.
Try:
print(float(distance))
For numbers with more decimals (e.g your example of 1.00E-06), you can force float notation all the time. Try:
print(format(float(distance), 'f'))
If you only want a certain number of decimals, try:
print(format(float(distance), '.2f'))
(for example)
The link you posted actually gives the answer, albeit in a roundabout way.
int() cannot parse strings that don't already represent integers.
But a scientific number is a float.
So, you need to cast it to float first to parse the string.
Try:
print(float(distance))
For numbers with more decimals (e.g your example of 1.00E-06), you can force float notation all the time. Try:
print(format(float(distance), 'f'))
If you only want a certain number of decimals, try:
print(format(float(distance), '.2f'))
(for example)
edited Nov 19 at 18:41
answered Nov 19 at 18:33
PhilB
815
815
Thanks that got me what I needed!
– A.will
Nov 19 at 20:14
Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted.
– PhilB
Nov 19 at 20:34
add a comment |
Thanks that got me what I needed!
– A.will
Nov 19 at 20:14
Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted.
– PhilB
Nov 19 at 20:34
Thanks that got me what I needed!
– A.will
Nov 19 at 20:14
Thanks that got me what I needed!
– A.will
Nov 19 at 20:14
Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted.
– PhilB
Nov 19 at 20:34
Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted.
– PhilB
Nov 19 at 20:34
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379165%2fconverting-a-scientific-notation-string-to-an-integer%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
What programming language are you using?
– Dragonthoughts
Nov 19 at 16:46
Cant believe I forgot to add that; I am using python (version 3.7). I have updated the post.
– A.will
Nov 19 at 18:25