2

I have wrote the following code to split a line, what I was trying to do is to get the 1.802e+05 and 1.739e+04. I think I can split the line with space, then I can get those values, but what I got so far is only the letter H. Anyone can show me where my bug is?

line = 'Htin     1.802e+05 [J kg^-1]    Htout    1.739e+04 [J kg^-1]'
line.split(' ')
print line[0]

6 Answers 6

2

line.split() return the result, which you haven't stored anywhere. So, that line has no effect. In the next line, line is still the string, so line[0] is H

line = 'Htin 1.802e+05 [J kg^-1] Htout 1.739e+04 [J kg^-1]' temp = line.split(' ') print temp[0]

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very mucu. But if the line is sometime like this line = 'Htin 1.802e+05 [J kg^-1] Htout -1.739e+04 [J kg^-1]', some values in line are negative. After splitting, the Htout and -1.739e+04 will combine together, do you have any idea to resolve this problem?
2

Personally, I'd skip the split and go right for a re solution -- e.g if every number you want to extract is in exponential notation,

numstrings = re.findall(r'\d\.\d+e[+-]\d+', line)

would work. Just adjust the RE pattern to the forms of numbers you want to extract!

Comments

1

Split doesn't alter line; it returns a list of strings. But even if it did, line[0] wouldn't give you one of the numbers.

Comments

1

You can use try/except:

>>> line = 'Htin     1.802e+05 [J kg^-1]    Htout    1.739e+04 [J kg^-1]'
>>> result=[]
>>> for e in line.split():
...    try:
...       result.append(float(e))
...    except ValueError:
...       pass
... 
>>> result
[180200.0, 17390.0]

Comments

1

line.split() will return a result which you are not storing anywhere. Using line[0] will give you the character at index 0 in the string line.

This should be more what you want:

>>> line = 'Htin     1.802e+05 [J kg^-1]    Htout    1.739e+04 [J kg^-1]'
>>> lines = line.split()
>>> lines
['Htin', '1.802e+05', '[J', 'kg^-1]', 'Htout', '1.739e+04', '[J', 'kg^-1]']
>>> lines[1]
'1.802e+05'
>>> lines[5]
'1.739e+04'

Comments

0
import re

line = 'Htin     1.802e+05 [J kg^-1]    Htout    1.739e+04 [J kg^-1]'
a=line.split()
for x in a:
    if re.findall("[0-9][0-9]",x):
        print (x)

Output:

>>> 
1.802e+05
1.739e+04
>>> 

re module for search what you want.

2 Comments

As I've already said, it doesn't make any sense to use split() if you're using re.findall(); please see @Alex Martelli's answer for how to correctly use the re module
@howaboutNO My downvote has nothing to do with others; it's not about correct or not; you can write 100 print statements to print number 1 to 100 and claim it's as correct as using a for loop, that's similar to what this answer does

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.