Python过滤文字
Python filtering text
你好这个文本文件将信息分组在字符“**********************************”之间例如:
*******************************
15674B 2000
#12 DIVERSION
800.000
COORDINATES
0
FLOW DIRECTION
0
PROTECT DATA
0
DATUM
0.00
RADIUS TYPE
2
DIVIDE X-Section
0
SECTION ID
INTERPOLATED
0
ANGLE
0.00 0
RESISTANCE NUMBERS
0 0 1.000 1.000 1.000 1.000 1.000
PROFILE 8
-15.000 12.000 1.000 <#0> 0 0.000 0
0.000 10.960 1.000 <#1> 0 0.000 0
0.600 10.820 1.000 <#0> 0 0.000 0
0.700 10.410 1.000 <#0> 0 0.000 0
1.540 9.990 1.000 <#0> 0 0.000 0
4.040 9.980 1.000 <#2> 0 0.000 0
6.200 11.160 1.000 <#4> 0 0.000 0
15.000 12.000 1.000 <#0> 0 0.000 0
LEVEL PARAMS
0 0 0.000 0 0.000 20
*******************************
15674B 2000
#12 DIVERSION
900.000
我想做的是提取字符“*********************”下方的第二行和第三行 (#12 DIVERSION, 800.00) **********”以及第 24 -32 行的 PROFILE 信息,并将它们保存到 csv 文件中。
我知道我可以使用python来读取文件例如:
with open ("results.txt","r") as myResults:
readFile = myResults.readlines()
但我的问题是我不知道如何识别字符之间的信息组“******************************* ***”,然后提取某些行。
如有任何帮助,我们将不胜感激。
这可能会起作用:
lines_you_want = []
with open ("test.txt","r") as myResults:
lines = myResults.readlines()
indexes_of_lines_you_want = [] # We create a list for the indexes of the lines you want to extract
for i in range(len(lines)):
if '*******' in lines[i]: # We check if the current line is a line full of stars
indexes_of_lines_you_want.extend([i+2, i+3]) # We add the indexes current_index+2 and current_index+3 to our list
for i in indexes_of_lines_you_want:
lines_you_want.append(lines[i])
之后,您可以将列表中的行 lines_you_want
保存到这样的 .csv 文件中
import csv
myfile = open('result.csv', 'w', newline='')
writer = csv.writer(myfile)
writer.writerow(lines_you_want)
尽管您可能应该将 import csv
放在开头。
如果文件不是太长(即你可以将整个文件读入内存),你可以这样尝试:
with open("results.txt","r") as myResults:
blocks = myResults.read() # put the whole file into a string
# split the string into blocks and process them independently
for block in blocks.split('*******************************')[1:]:
lines = block.split('\n')
print lines[1]
print lines[2]
for i in range(24, 33):
print lines[i]
你好这个文本文件将信息分组在字符“**********************************”之间例如:
*******************************
15674B 2000
#12 DIVERSION
800.000
COORDINATES
0
FLOW DIRECTION
0
PROTECT DATA
0
DATUM
0.00
RADIUS TYPE
2
DIVIDE X-Section
0
SECTION ID
INTERPOLATED
0
ANGLE
0.00 0
RESISTANCE NUMBERS
0 0 1.000 1.000 1.000 1.000 1.000
PROFILE 8
-15.000 12.000 1.000 <#0> 0 0.000 0
0.000 10.960 1.000 <#1> 0 0.000 0
0.600 10.820 1.000 <#0> 0 0.000 0
0.700 10.410 1.000 <#0> 0 0.000 0
1.540 9.990 1.000 <#0> 0 0.000 0
4.040 9.980 1.000 <#2> 0 0.000 0
6.200 11.160 1.000 <#4> 0 0.000 0
15.000 12.000 1.000 <#0> 0 0.000 0
LEVEL PARAMS
0 0 0.000 0 0.000 20
*******************************
15674B 2000
#12 DIVERSION
900.000
我想做的是提取字符“*********************”下方的第二行和第三行 (#12 DIVERSION, 800.00) **********”以及第 24 -32 行的 PROFILE 信息,并将它们保存到 csv 文件中。
我知道我可以使用python来读取文件例如:
with open ("results.txt","r") as myResults:
readFile = myResults.readlines()
但我的问题是我不知道如何识别字符之间的信息组“******************************* ***”,然后提取某些行。
如有任何帮助,我们将不胜感激。
这可能会起作用:
lines_you_want = []
with open ("test.txt","r") as myResults:
lines = myResults.readlines()
indexes_of_lines_you_want = [] # We create a list for the indexes of the lines you want to extract
for i in range(len(lines)):
if '*******' in lines[i]: # We check if the current line is a line full of stars
indexes_of_lines_you_want.extend([i+2, i+3]) # We add the indexes current_index+2 and current_index+3 to our list
for i in indexes_of_lines_you_want:
lines_you_want.append(lines[i])
之后,您可以将列表中的行 lines_you_want
保存到这样的 .csv 文件中
import csv
myfile = open('result.csv', 'w', newline='')
writer = csv.writer(myfile)
writer.writerow(lines_you_want)
尽管您可能应该将 import csv
放在开头。
如果文件不是太长(即你可以将整个文件读入内存),你可以这样尝试:
with open("results.txt","r") as myResults:
blocks = myResults.read() # put the whole file into a string
# split the string into blocks and process them independently
for block in blocks.split('*******************************')[1:]:
lines = block.split('\n')
print lines[1]
print lines[2]
for i in range(24, 33):
print lines[i]