【问题标题】:Splitting data from a txt file从 txt 文件中拆分数据
【发布时间】:2017-04-12 07:34:20
【问题描述】:

我是 Python 新手。

我想要做的是 split 我从 txt 文件中得到的内容来选择 Aperture 和 ShutterSpeed 值。

这就是我的数据的样子(30 种不同的光圈和快门速度值):

======== /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]

有什么建议吗?

【问题讨论】:

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


【解决方案1】:

您可以使用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)

以上代码 sn-ps 都会产生这个结果:

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

【讨论】:

  • scontent.xx.fbcdn.net/v/t35.0-12/…这是我的txt数据的确切形式,我需要三个参数,光圈(例如2.2)快门(例如1)和速度(例如1333)跨度>
  • @MouradOverFlow 我已经根据\n分割的数据添加了一个解决方案。
  • 完美!谢谢先生。
  • 被选为答案。非常比你
【解决方案2】:

看来您实际上已经实现了目标。这是你的代码sn-p的标题:

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 的样式

【讨论】:

  • 谢谢你,但我不确定这是数据的形式,我会告诉你我拥有的数据的确切形式,以及我想要排序的确切内容......
  • 这里可以看到txt文件对不起scontent.xx.fbcdn.net/v/t35.0-12/…
  • 所以我需要三个参数,光圈(例如2.2)快门(例如1)和速度(例如1333)
【解决方案3】:

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

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 :”开头,后跟任意数量的数字,然后是一个点,然后是任意数量的数字。 对于快门速度,代码的工作方式完全相同。

【讨论】:

    【解决方案4】:

    我找到了使用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")
    

    【讨论】:

    • 谢谢你的回答,我没听懂?如何使用此代码。
    猜你喜欢
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多