拆分txt文件中的数据

时间:2017-04-12 07:34:21

标签: python-2.7 python-3.x data-analysis

我是Python的新手。

我要做的是splittxt文件中获取的内容,只选择 Aperture ShutterSpeed

这就是我的数据的样子(30种不同的Aperture和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

我需要让我的代码只选择浮点值(2.2和1/1884,例如来自所有数据)。

这是我要做的代码(在这里有些人的帮助下):

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]

有什么建议吗?

4 个答案:

答案 0 :(得分:2)

您可以使用slice operator)过滤掉您需要的文字,如下所示:

# -*- 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)

或者,您可以在不必对数据使用.split()的情况下执行此操作:

# -*- 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)

以上两个代码段都会产生这样的结果:

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

编辑:由于您为一个实体编制了三个不同索引的数据,因此可以使用切片运算符的step来获取并处理,如下所示:

# -*- 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)

这将导致:

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

或者,您可以格式化最终结果:

# -*- 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)))))

这将导致:

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

答案 1 :(得分:1)

好像你实际上已经实现了目标。这是您的代码段的标题:

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

现在,您可以获取字符串列表,每个字符串中都有独特模式。让我们split it up进入大块并解剖这些片段:

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)

这适用于Python 3.x如果您正在使用第二个版本 - 只需重新设置print function

的样式

答案 2 :(得分:1)

看看我使用正则表达式的方法:

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]

输出提供的字符串:

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

因为有' /'在快门值我把它留作一个字符串。

一些解释:

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

此行查找所有出现的字符序列(使用regex expression),以文字' Aperature开头:'然后是任意数量的数字,然后是一个点,然后是任意数量的数字。 对于快门速度,代码的工作方式完全相同。

答案 3 :(得分:0)

我找到了使用this模块的解决方案。这是代码:

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")
相关问题