如何使用 PYthon 获取两行之间的文本
How to get text between 2 lines with PYthon
所以我有一个结构如下的文本文件:
Product ID List:
ABB:
578SH8
EFC025
EFC967
CNC:
HDJ834
HSLA87
...
...
此文件继续,许多公司的名称和 Id 在它们下面。然后我需要获取所选公司的 ID 并将它们附加到列表中,它们将用于搜索网站。这是我必须获取数据的当前行:
PID = open('PID.txt').read().split()
如果其中只有 1 个公司的产品 ID 并且没有文本,这将非常有用。然而,这对我计划做的事情不起作用......我怎样才能让 reader 在它说 ABB:
之后从(一个例子)读取到下一家公司之前?我在想也许可以在文件中添加一些东西,比如 ABB END
来知道要切到哪里,但我仍然不知道如何首先在行之间切出......如果你能让我知道了,那就太好了!
由于文件的结构与此类似,您可以按照以下步骤操作:
- 根据两个换行符
\n\n
拆分成列表
- 在单个换行符上拆分每个列表
\n
- 删除包含每个公司 ID 的列表的第一个元素
- 根据需要为公司名称使用第一个元素(如上所述)(确保删除冒号)
此外,请查看 regular expressions 以了解如何解析这样的数据。
with open('file.txt', 'r') as f: # open the file
next(f) # skip the first line
results = {} # initialize a dictionary
for line in f: # iterate through the remainder of the file
if ':' in line: # if the line contains a :
current = line.strip() # strip the whitespace
results[current] = [] # and add it as a dictionary entry
elif line.strip(): # otherwise, and if content remains after stripping whitespace,
results[current].append(line.strip()) # append this line to the relevant list
两个连续的新行作为分隔符,所以就坐在那里构造一个数据字典:
data = {i.split()[0]: i.split()[1:] for i in open('PID.txt').read().split('\n\n')}
这至少应该让您入门,使用字典可能比使用列表更幸运,至少对于逻辑的第一部分而言。您将通过什么方式传递代码?
a = {}
f1 = open("C:\sotest.txt", 'r')
current_key = ''
for row in f1:
strrow = row.strip('\n')
if strrow == "":
pass
elif ":" in strrow:
current_key = strrow.strip(':')
a[current_key] = []
else:
a[current_key].append(strrow)
for key in a:
print key
for item in a[key]:
print item
所以我有一个结构如下的文本文件:
Product ID List:
ABB:
578SH8
EFC025
EFC967
CNC:
HDJ834
HSLA87
...
...
此文件继续,许多公司的名称和 Id 在它们下面。然后我需要获取所选公司的 ID 并将它们附加到列表中,它们将用于搜索网站。这是我必须获取数据的当前行:
PID = open('PID.txt').read().split()
如果其中只有 1 个公司的产品 ID 并且没有文本,这将非常有用。然而,这对我计划做的事情不起作用......我怎样才能让 reader 在它说 ABB:
之后从(一个例子)读取到下一家公司之前?我在想也许可以在文件中添加一些东西,比如 ABB END
来知道要切到哪里,但我仍然不知道如何首先在行之间切出......如果你能让我知道了,那就太好了!
由于文件的结构与此类似,您可以按照以下步骤操作:
- 根据两个换行符
\n\n
拆分成列表 - 在单个换行符上拆分每个列表
\n
- 删除包含每个公司 ID 的列表的第一个元素
- 根据需要为公司名称使用第一个元素(如上所述)(确保删除冒号)
此外,请查看 regular expressions 以了解如何解析这样的数据。
with open('file.txt', 'r') as f: # open the file
next(f) # skip the first line
results = {} # initialize a dictionary
for line in f: # iterate through the remainder of the file
if ':' in line: # if the line contains a :
current = line.strip() # strip the whitespace
results[current] = [] # and add it as a dictionary entry
elif line.strip(): # otherwise, and if content remains after stripping whitespace,
results[current].append(line.strip()) # append this line to the relevant list
两个连续的新行作为分隔符,所以就坐在那里构造一个数据字典:
data = {i.split()[0]: i.split()[1:] for i in open('PID.txt').read().split('\n\n')}
这至少应该让您入门,使用字典可能比使用列表更幸运,至少对于逻辑的第一部分而言。您将通过什么方式传递代码?
a = {}
f1 = open("C:\sotest.txt", 'r')
current_key = ''
for row in f1:
strrow = row.strip('\n')
if strrow == "":
pass
elif ":" in strrow:
current_key = strrow.strip(':')
a[current_key] = []
else:
a[current_key].append(strrow)
for key in a:
print key
for item in a[key]:
print item