python - Read text file as a whole -


this question has answer here:

i need help. want read text file "as whole" , not line line. because doing line line regex doesn't work well, needs whole text. far being doing:

with open(r"alltext.txt") fp:     line in fp:         in re.finditer(regexp_v3, line):             print i.group() 

i need open file, read all, search if regex , print results. how can accomplish this?

for use read:

with open("alltext.txt") fp:     whole_file_text = fp.read() 

note however, test contain \n new-line used in text.

for example, if text file:

#alltext.txt hello how 

your whole_file_text string follows:

>>> whole_file_text 'hello\nhow\nare\nyou' 

you can either of following:

>>> whole_file_text.replace('\n', ' ') 'hello how you'  >>> whole_file_text.replace('\n', '') 'hellohowareyou' 

Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -