我有一个文件,它用“\ n”分段,每段的行数未知。该文件的示例如下所示:
800004
The London and North-Western's Euston Station was first, but at the eastern end of Euston Road the Great Northern constructed their King's Cross terminal.
Initially the Midland Railway ran into King's Cross but a quarrel over access led them to construct next door to King's Cross their St Pancras terminal, which was topped by a statue of Britannia, a <tag "510285">calculated</> snook-cocking exercise because Britannia was the company emblem of the Midland's hated rival, the London and North-Western.
800005
GROWTH in Malaysia's gross domestic product this year is expected to be 8.5 per cent.
Nearly two percentage points higher than the Treasury's estimate, Bank Negara, the central bank, reported yesterday.
Last year's growth, <tag "510270">calculated</> by the bank, was 8.7 per cent, compared with 7.6 per cent by the Treasury.
800006
He was a Catholic.
When he visited the Pope, even then, he couldn't help <tag "510270">calculating</> the Pope's worldly riches (life-proprietor of the Sistine Chapel, landlord of the Vatican and contents &ellip. ).
是否有更简单的方法从文本文件中获取段?
我一直这样做:
doc = []
segments = []
for line in open(trainfile):
if line == "\n":
doc.append(segments)
segments = []
else:
segments.append(line.strip())
for i in doc:
print i
答案 0 :(得分:6)
使用生成器功能:
def per_section(it):
section = []
for line in it:
if line.strip():
section.append(line)
else:
yield ''.join(section)
section = []
# yield any remaining lines as a section too
if section:
yield ''.join(section)
这会产生每个部分,用空行分隔,作为一个字符串:
with open(sectionedfile, 'r') as inputfile:
for section in per_section(inputfile):
print section
答案 1 :(得分:3)
itertools.groupby
似乎是你的朋友:
for k,section in groupby(file,key=str.isspace):
if k:
for line in section:
...
答案 2 :(得分:0)
如果文件不是很大,那么您也可以使用str.split
并在'\n\n'
拆分:
如果文件很大,请使用@Martijn Pieters建议的方法
with open('abc') as f:
data = f.read()
segments = data.split('\n\n')
...
for x in segments:
print '--->',x
<强>输出:强>
---> 800004
The London and North-Western's Euston Station was first, but at the eastern end of Euston Road the Great Northern constructed their King's Cross terminal.
Initially the Midland Railway ran into King's Cross but a quarrel over access led them to construct next door to King's Cross their St Pancras terminal, which was topped by a statue of Britannia, a <tag "510285">calculated</> snook-cocking exercise because Britannia was the company emblem of the Midland's hated rival, the London and North-Western.
---> 800005
GROWTH in Malaysia's gross domestic product this year is expected to be 8.5 per cent.
Nearly two percentage points higher than the Treasury's estimate, Bank Negara, the central bank, reported yesterday.
Last year's growth, <tag "510270">calculated</> by the bank, was 8.7 per cent, compared with 7.6 per cent by the Treasury.
---> 800006
He was a Catholic.
When he visited the Pope, even then, he couldn't help <tag "510270">calculating</> the Pope's worldly riches (life-proprietor of the Sistine Chapel, landlord of the Vatican and contents &ellip. ).