1

I am new to Python.

What I am trying to do is to split what I got from a txt file to select just the Aperture and the ShutterSpeed values.

This is how my data looks like (30 different values of Aperture and Shutter Speed) :

======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083601_0004_RED.TIF
Aperture                        : 2.2
Shutter Speed                   : 1/1806
======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083600_0003_NIR.TIF
Aperture                        : 2.2
Shutter Speed                   : 1/510
======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083601_0004_REG.TIF
Aperture                        : 2.2
Shutter Speed                   : 1/374
======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083600_0003_RED.TIF
Aperture                        : 2.2
Shutter Speed                   : 1/1884

I need to make my code select only the float values (2.2, and 1/1884 for example from all the data).

This is the code I am trying to do (with the help of some people here) :

filename='/home/stagiaire/Bureau/datatest.txt'
with open(filename) as f:
    data = f.read()
data = data.split('\n')

Fnumber      = [float(row.split(':')[0]) for row in data]
ShutterSpeed = [float(row.split(':')[1]) for row in data]

Any suggestions?

Satish Prakash Garg
  • 2,213
  • 2
  • 16
  • 25
Mourad Over Flow
  • 191
  • 1
  • 5
  • 16
  • This is the form of the data https://scontent.xx.fbcdn.net/v/t35.0-12/17902196_10212672890847136_1560918175_o.png?oh=c0f0b236cde03da6d5012d2bbf17233d&oe=58F092B3 – Mourad Over Flow Apr 12 '17 at 12:43

4 Answers4

2

You can use slice operator(:) to filter out the text you need, something like this :

# -*- coding: utf-8 -*-
data = [
        "======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083601_0004_RED.TIF Aperture : 2.2 Shutter Speed : 1/1806",
        "======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083600_0003_NIR.TIF Aperture : 2.2 Shutter Speed : 1/510", 
        "======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083601_0004_REG.TIF Aperture : 2.2 Shutter Speed : 1/374",
        "======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083600_0003_RED.TIF Aperture : 2.2 Shutter Speed : 1/1884"
       ]
for node in data : 
    node_lst = node[node.index('Aperture : '):].split()
    Fnumber = node_lst[2]
    ShutterSpeed = node_lst[6]
    print(Fnumber, ShutterSpeed)

Alternatively, you can do this without having to use .split() on your data :

# -*- coding: utf-8 -*-
data = [
        "======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083601_0004_RED.TIF Aperture : 2.2 Shutter Speed : 1/1806",
        "======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083600_0003_NIR.TIF Aperture : 2.2 Shutter Speed : 1/510", 
        "======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083601_0004_REG.TIF Aperture : 2.2 Shutter Speed : 1/374",
        "======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083600_0003_RED.TIF Aperture : 2.2 Shutter Speed : 1/1884"
       ]

for node in data : 
    Fnumber_txt = node[node.index('Aperture : ') + len('Aperture : '):]
    Fnumber = Fnumber_txt[:Fnumber_txt.index(' ')]
    ShutterSpeed = node[node.index('Shutter Speed : ') + len('Shutter Speed : '):]
    print(Fnumber, ShutterSpeed)

Both of the above code snippets will produce this result :

2.2 1/1806
2.2 1/510
2.2 1/374
2.2 1/1884

Edit : As you have data at three different indexed for one entity, you can use step of slice operator to fetch it and process, something like this :

# -*- coding: utf-8 -*-
data = [
        "======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083601_0004_RED.TIF", 
        "Aperture                        : 2.2", 
        "Shutter Speed                   : 1/1806",
        "======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083600_0003_NIR.TIF", 
        "Aperture                        : 2.2", 
        "Shutter Speed                   : 1/510", 
        "======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083601_0004_REG.TIF", 
        "Aperture                        : 2.2", 
        "Shutter Speed                   : 1/374",
        "======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083600_0003_RED.TIF", 
        "Aperture                        : 2.2", 
        "Shutter Speed                   : 1/1884"
       ]

fns = [float(fn.split(":")[-1].strip()) for fn in data[1::3]]
sss = [ss.split(":")[-1].strip().split("/") for ss in data[2::3]]

for i, elems in enumerate(sss) : 
    Fnumber = fns[i]
    Shutter = elems[0]
    Speed = elems[1]

    print(Fnumber)
    print(Shutter)
    print(Speed)

This will result in :

2.2
1
1806
2.2
1
510
2.2
1
374
2.2
1
1884

Alternatively, you can format your final result like this :

# -*- coding: utf-8 -*-
data = [
        "======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083601_0004_RED.TIF", 
        "Aperture                        : 2.2", 
        "Shutter Speed                   : 1/1806",
        "======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083600_0003_NIR.TIF", 
        "Aperture                        : 2.2", 
        "Shutter Speed                   : 1/510", 
        "======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083601_0004_REG.TIF", 
        "Aperture                        : 2.2", 
        "Shutter Speed                   : 1/374",
        "======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083600_0003_RED.TIF", 
        "Aperture                        : 2.2", 
        "Shutter Speed                   : 1/1884"
       ]

fns = [float(fn.split(":")[-1].strip()) for fn in data[1::3]]
sss = [ss.split(":")[-1].strip().split("/") for ss in data[2::3]]
print(list(map(lambda x: [float(x[0]), float(x[1][0]), float(x[1][1])], list(zip(fns, sss)))))

This will result in :

[[2.2, 1.0, 1806.0], [2.2, 1.0, 510.0], [2.2, 1.0, 374.0], [2.2, 1.0, 1884.0]]

Satish Prakash Garg
  • 2,213
  • 2
  • 16
  • 25
1

Seems like you're practically achieve your goal. This is a header of your code snippet:

filename='/home/stagiaire/Bureau/datatest.txt'
with open(filename) as f:
    data = f.read()
list_of_strings = data.split('\n')

Now you're get the list of strings each one has a distinctive pattern inside. Let's split it up onto the chunks and dissect these flakes:

for i in list_of_strings:
    # now split it into 2 parts and get the tail:
    gist=row.split('Aperture:'[-1].strip()
    print("This is a gist out of string:", gits)
    # split and get the head of result:
    aperture=float(gist.split()[0])               
    print("Aperture:", aperture)
    # and now the speed:
    shutter_speed = gist.split()[-1] 
    print("Shutter speed:", shutter_speed)

This is for Python 3.x If you're using the second version - just re-style the print function

Alioth
  • 595
  • 4
  • 11
  • Thank you sure, but I am not sure this is the form of the data, I will show you the exact form of the data I have, and the exact thing I want to sort... – Mourad Over Flow Apr 12 '17 at 12:03
  • You can see the txt file here I am sorry https://scontent.xx.fbcdn.net/v/t35.0-12/17902196_10212672890847136_1560918175_o.png?oh=c0f0b236cde03da6d5012d2bbf17233d&oe=58F092B3 – Mourad Over Flow Apr 12 '17 at 12:09
  • So I need to have three parameters, Apertue (for example 2.2) Shutter(for example 1) and Speed (for example 1333) – Mourad Over Flow Apr 12 '17 at 12:10
1

Take a look at my approach with regex:

import re

string = r"======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083601_0004_RED.TIF Aperture : 2.2 Shutter Speed : 1/1806 ======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083600_0003_NIR.TIF Aperture : 2.2 Shutter Speed : 1/510 ======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083601_0004_REG.TIF Aperture : 2.2 Shutter Speed : 1/374 ======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083600_0003_RED.TIF Aperture : 2.2 Shutter Speed : 1/1884"

aperatures = re.findall(r'Aperture : \d*\.\d*', string)
aperatures_float = [float(aperature.split(sep=':')[1].strip()) for aperature in aperatures]
shutter_speeds = re.findall(r'Shutter Speed : \d*\/\d*', string)
shutter = [shutter.split(sep=':')[1].strip() for shutter in shutter_speeds]

And output for provided string:

In[332]:
shutter
Out[332]: 
['1/1806', '1/510', '1/374', '1/1884']
type(shutter[0])
Out[333]: 
str

In[328]:
aperatures_float
Out[328]: 
[2.2, 2.2, 2.2, 2.2]
aperatures_float[0]
Out[329]: 
2.2
type(aperatures_float[0])
Out[330]: 
float

Since there is '/' in shutter value I left it as a string.

Some explanations:

re.findall(r'Aperture : \d*\.\d*', string)

this line finds all occurrences of character sequences (using regex expression) that start with literal 'Aperature : ' and is followed by any number of digits, then a dot and then any number of digits again. For shutter speeds code works exactly the same.

dylan_fan
  • 680
  • 1
  • 5
  • 18
0

I have found a solution using this module. Here is the code:

import re

re.findall(r"[-+]?\d*\.\d+|\d+", "======== /home/stagiaire/Bureau/Photos Test 
Luxmetes/Position 2 (au sol à l'ombre)/0033/IMG_170407_083601_0004_RED.TIF 
Aperture : 2.2 Shutter Speed : 1/1806 ======== /home/stagiaire/Bureau/Photos 
Test Luxmetes/Position 2 (au sol à 
l'ombre)/0033/IMG_170407_083600_0003_NIR.TIF Aperture : 2.2 Shutter Speed : 
1/510 ======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 2 (au 
sol à l'ombre)/0033/IMG_170407_083601_0004_REG.TIF Aperture : 2.2 Shutter 
Speed : 1/374 ======== /home/stagiaire/Bureau/Photos Test Luxmetes/Position 
2 (au sol à l'ombre)/0033/IMG_170407_083600_0003_RED.TIF Aperture : 2.2 
Shutter Speed : 1/1884")
Vagif
  • 224
  • 1
  • 3
  • 17