簡體   English   中英

如何獲取文本文件中兩行之間的數據?

[英]How can i take the data between two line in a text file?

我有一個名為 DATA_out 的復雜文本格式輸出文件(如下面的示例),我想在文件內的兩行(例如值和總計)數據之間獲取並另存為 csv。 我用用戶輸入和結束行做了一個起始行。 當我的代碼運行時,它知道從哪里開始寫入,但我無法導入結束行(“總計”)。 我只需要添加start_lineend_linere.compile

您對使用 USER INPUT 在兩行之間獲取數據有什么建議嗎? 這是我所擁有的。

DATA_out file
      values
    DATA_LINE 1
    DATA_LINE 2
    DATA_LINE 3
    DATA_LINE 4
total

# Spyder Editor (Python 3.7)
import pandas as pd
import re

start_line = input('Starting:')
end_line = 'total' # end point.

with open('DATA_out.txt','r') as file:
    input = file.read()

rexp = re.compile(start_line,re.DOTALL) # need to add between start and end
match = rexp.search(input)
result = '' if match == None else match.group(1)
with open('NEW_FILE.txt','w') as file:
    file.write(result)

使用正則表達式,您可以使用'values(.*)total''\\n' - 'values\\n(.*)\\ntotal'

text = '''DATA_out file
      values
    DATA_LINE 1
    DATA_LINE 2
    DATA_LINE 3
    DATA_LINE 4
total
'''

import re

result = re.search('values(.*)total', text, re.DOTALL)

if result:
    print(result[1])
    #print(result.group(1))    

如果沒有regex您可以使用find()來分別查找valuestotal位置,然后用text[start:end]對其進行切片

text = '''DATA_out file
      values
    DATA_LINE 1
    DATA_LINE 2
    DATA_LINE 3
    DATA_LINE 4
total
'''

start = text.find('values')
end = text.find('total', start)

if start > -1 and end > -1:
    start += len("values")  
    print(text[start:end])

如果要逐行讀取文件。

我使用io.StringIO()來模擬文件

text = '''DATA_out file
      values
    DATA_LINE 1
    DATA_LINE 2
    DATA_LINE 3
    DATA_LINE 4
total
'''

import io

#f = open("input.txt")
f = io.StringIO(text)

lines = []

# read till you find line with 'values'
for line in f:
    if 'values' in line:
        break

# read till you find line with 'values'
for line in f:
    if 'total' in line:
        break
    lines.append(line)
else: # it is `for/else`, not `if/else`
    #if not found `total` (so there was no `break`) then clear list
    lines = []    

if lines:
    print("".join(lines))    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM