[英]Extracting data from a text file with Python
所以我有一個大文本文件。 它包含以下格式的一堆信息:
|NAME|NUMBER(1)|AST|TYPE(0)|TYPE|NUMBER(2)||NUMBER(3)|NUMBER(4)|DESCRIPTION|
對不起,模糊不清。 所有信息的格式如上,每個描述符之間是分隔符“|”。 我希望能夠在文件中搜索“NAME”並在其自己的標簽中打印每個描述符,例如:
Name
Number(1):
AST:
TYPE(0):
etc....
如果我仍然感到困惑,我希望能夠搜索名稱,然后打印出每個被“|”分隔的信息。
有人可以幫忙嗎?
編輯以下是文本文件的一部分示例:
| Trevor Jones | 70 | AST |白色|地球| 3 || 500 | 1500 |老人住在養老院|
這是我到目前為止的代碼:
with open('LARGE.TXT') as fd:
name='Trevor Jones'
input=[x.split('|') for x in fd.readlines()]
to_search={x[0]:x for x in input}
print('\n'.join(to_search[name]))
就像是
#Opens the file in a 'safe' manner
with open('large_text_file') as fd:
#This reads in the file and splits it into tokens,
#the strip removes the extra pipes
input = [x.strip('|').split('|') for x in fd.readlines()]
#This makes it into a searchable dictionary
to_search = {x[0]:x for x in input}
然后搜索
to_search[NAME]
根據您希望使用的答案的格式
print ' '.join(to_search[NAME])
要么
print '\n'.join(to_search[NAME])
一句警告,這個解決方案假設名稱是唯一的,如果它們不是一個更復雜的解決方案可能是必需的。
首先,你需要以某種方式打破文件。 我認為字典是最好的選擇。 然后你就可以得到你需要的東西。
d = {}
# Where `fl` is our file object
for L in fl:
# Skip the first pipe
detached = L[1:].split('|')
# May wish to process here
d[detached[0]] = detached[1:]
# Can do whatever with this information now
print d.get('string_to_search')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.