Search code examples
python-2.7python-3.xdata-analysis

Splitting data from a txt file


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?


Solution

  • 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]]