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










share|improve this question




















  • 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















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










share|improve this question




















  • 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













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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












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)






share|improve this answer























  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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)






share|improve this answer























  • 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















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)






share|improve this answer























  • 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













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)






share|improve this answer














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)







share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin