【问题标题】:Extract text from file over separate/separated lines in Python在 Python 中通过单独/分隔的行从文件中提取文本
【发布时间】:2021-03-26 10:05:01
【问题描述】:

希望有人可以提供帮助,因为我对 Python 还很陌生并且有点挣扎。

我需要获取一个文本文件的内容并将其元素提取到另一个文件中,捕获一些行但忽略其他行。例如,这是原始文件的一部分:

input1   
   name "Bob"   
   Always_active  
next_input  
input2   
   name "Alice"   
   Sometimes_active   
next_input  
input3   
   name "Ted"   
   Always_active   
next_input  
input4    
   name "Albert"   
   Never_active   
next_input  
input5   
   name "Sue"   
   Always_active   
next_input  
input6   
   name "David"   
   Never_active   
next_input  
input7   
   name "Building1"   
   Always_active   
next_input  
input8   
   name "Building2"   
   Always_active   
next_input  
input9   
   name "Building3"   
   Always_active   
next_input  
input10   
   name "Building4"     
   Always_active     
next_input  

这就是我希望能够捕捉到的:

input1   
   name "Bob"   
input2   
   name "Alice"   
input3   
   name "Ted"   
input4   
   name "Albert"   
input5   
   name "Sue"   
input6   
   name "David"   
input7   
   name "Building1"   
input8    
   name "Building2"   
input9   
   name "Building3"   
input10   
   name "Building4"    
  

所以基本上我需要忽略一些行并捕获其余部分。我怎样才能做到这一点?

【问题讨论】:

  • 谢谢大家,每个答案都非常有帮助

标签: python text extract


【解决方案1】:

您可以从阅读文件开始:

with open(r'C:\your_file_path\your_file_name.txt', 'r') as f:
    text = f.read().splitlines()

那么你将有一个变量“text”,它是一个列表,这个列表中的每个元素都是你的文本中的一行。接下来,您应该检查每一行的“条件”

new_text = []
for i in text:
    if i[:5] == 'input':
        new_text.append(i)
    elif i[:6] == 'name \"':
        new_text.append(i)

变量“new_text”是一个列表,其中包含您想要的文本元素行。

【讨论】:

    【解决方案2】:

    您应该逐行读取文件并过滤您想要将它们写入新文件的行。

    # First read file
    with open(filename, 'r') as f:
        file = f.readlines()
    
    # Create new file
    f2 = open(filename, 'a')
    
    for line in file:
        if line not in ('Always_active', 'Never_active'): # Filter lines you don't want
            f2.write(line) # Write line
            f2.write('\n') # Break line
    
    # Close file
    f2.close()
    

    应该有效

    【讨论】:

      【解决方案3】:

      您可以使用 pandas 来读取和编辑文件。

      假设您将文本保存在 temp_txt_file.txt 中,与您的 python 脚本位于同一文件夹中。

      import pandas as pd
      import csv
      
      data = pd.read_csv("./temp_txt_file.txt", header=None)
      data_filtered = data.loc[data.loc[:, 0].apply(lambda x: x.find("name") == 0 or x.find("input") == 0)]
      data_filtered.to_csv(path_or_buf='./filtered_data.txt', index=False, header=False, quoting=csv.QUOTE_NONE)
      

      您首先将数据加载到 pandas.DataFrame 中。在第二行中,您loc查找字符串以 nameinput 开头的所有行(这是由 .find() 完成的,它如果字符串以查询开头,则返回 0)

      注意: 使用 .apply(lambda x: function) 您可以将函数应用于 pandas.DataFrame 的每一行,其中 x 包含行作为字符串。

      代码:

      data.loc[:, 0].apply(lambda x: x.find("name") == 0 or x.find("input") == 0)
      

      产生一系列“真/假”,您可以将其视为过滤掉不需要的行的掩码。然后,您在 pandas.DataFrame 中应用(locate)它并将其保存到一个新文件 *filtered_data.txt" 中,没有任何 pandas.DataFrame 的标题和索引。

      注意: qutoing=csv.QUOTE_NONE 是为了避免不必要的“转义字符”

      建议:如果您可以以不同的方式格式化文本文件并使用 .csv 文件,这将很有帮助,因为您的数据似乎有点像表格。然后,您可以深入研究非常适合组织数据的 pandas

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-20
        • 2023-03-25
        • 2021-02-13
        • 1970-01-01
        • 2018-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多