【发布时间】:2016-09-16 07:51:43
【问题描述】:
我对 Python 还很陌生。我有一个文本文件,其中包含许多以下格式的数据块以及其他不必要的块。
NOT REQUIRED :: 123
Connected Part-1:: A ~$
Connected Part-3:: B ~$
Connector Location:: 100 200 300 ~$
NOT REQUIRED :: 456
Connected Part-2:: C ~$
我希望提取与每个属性(连接的第 1 部分,连接器位置)对应的信息(A、B、C、100 200 300)并将其存储为列表以供以后使用。我准备了以下代码,它读取文件、清理行并将其存储为列表。
import fileinput
with open('C:/Users/file.txt') as f:
content = f.readlines()
for line in content:
if 'Connected Part-1' in line or 'Connected Part-3' in line:
if 'Connected Part-1' in line:
connected_part_1 = [s.strip(' \n ~ $ Connected Part -1 ::') for s in content]
print ('PART_1:',connected_part_1)
if 'Connected Part-3' in line:
connected_part_3 = [s.strip(' \n ~ $ Connected Part -3 ::') for s in content]
print ('PART_3:',connected_part_3)
if 'Connector Location' in line:
# removing unwanted characters and converting into the list
content_clean_1 = [s.strip('\n ~ $ Connector Location::') for s in content]
#converting a single string item in list to a string
s = " ".join(content_clean_1)
# splitting the string and converting into a list
weld_location= s.split(" ")
print ('POSITION',weld_location)
这是输出
PART_1: ['A', '\t\tConnector Location:: 100.00 200.00 300.00', '\t\tConnected Part-3:: C~\t']
POSITION ['d', 'Part-1::', 'A', '\t\tConnector', 'Location::', '100.00', '200.00', '300.00', '\t\tConnected', 'Part-3::', 'C~\t']
PART_3: ['1:: A', '\t\tConnector Location:: 100.00 200.00 300.00', '\t\tConnected Part-3:: C~\t']
从这个程序的输出中,我可以得出结论,因为“内容”是由文件中所有字符组成的字符串,所以程序没有读取单独的行。相反,它将所有文本视为单个字符串。在这种情况下有人可以帮忙吗?
我期待以下输出:
PART_1: ['A']
PART_3: ['C']
POSITION: ['100.00', '200.00','300.00']
(注意)当我使用包含单行数据的单个文件时,它工作正常。抱歉问了这么长的问题
【问题讨论】:
-
你为什么要检查
if 'Connected Part-1' in line or 'Connected Part-3' in line:然后再检查嵌套的 if ?为什么不只是if 'Connected Part-1' in line:然后elif Connected Part-3' in line:和 if/or if?
标签: python python-3.x