I am stucked at a problem where I have to extract some characters from the string .
data
[' 10001296 58.38525-26.136000003431 348. 0 0\n',
' 10001297 58.975-26.400000003465 344. 0 0\n',
' 10001298 58.975-26.400000003465 340. 0 0\n',
' 10001299 58.975-26.400000003465 336. 0 0\n',
' 10001300 58.975-26.400000003465 332. 0 0\n',
' 10001301 58.975-26.400000003465 328. 0 0\n',
' 10001302 58.975-26.400000003465 324. 0 0\n',
' 10001303 58.975-26.400000003465 320. 0 0\n']
From above data, I want to extract the character number 11 to 36 from each line and want to store in a list. How can I do that?? In above example, the characters I want to achive is 58.38525
and similary in other lines below also.
I know how to extract the first 10 characters. But I do not know how to achieve characters from the middle of the string.
CodePudding user response:
That's pretty basic use of string manipulations:
data[0][11:36]
Will give what you want for 1st line.
If you want for each line, make a loop over:
data[i][11:36]
CodePudding user response:
Try:
data = [
" 10001296 58.38525-26.136000003431 348. 0 0\n",
" 10001297 58.975-26.400000003465 344. 0 0\n",
" 10001298 58.975-26.400000003465 340. 0 0\n",
" 10001299 58.975-26.400000003465 336. 0 0\n",
" 10001300 58.975-26.400000003465 332. 0 0\n",
" 10001301 58.975-26.400000003465 328. 0 0\n",
" 10001302 58.975-26.400000003465 324. 0 0\n",
" 10001303 58.975-26.400000003465 320. 0 0\n",
]
out = [line[11:36].strip() for line in data]
print(out)
Prints:
[
"58.38525-26.136000",
"58.975-26.400000",
"58.975-26.400000",
"58.975-26.400000",
"58.975-26.400000",
"58.975-26.400000",
"58.975-26.400000",
"58.975-26.400000",
]