M
M
Matthew Stafford2018-10-17 21:12:16
Python
Matthew Stafford, 2018-10-17 21:12:16

Outputting a line that starts with...?

Hello! I have a text document that I have already opened and read with Python, now I want to count the number of lines that start with 123(text) for example. That is, they start with 123 and what is in brackets no longer matters.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
igorzakhar, 2018-10-17
@matthewstafford

with open('file', 'r') as fp:
  print len([i for i in fp if i.startswith('123')])

M
mark okolov, 2018-10-17
@okolovmark

as an option, you translate its contents into str and compare with the first three characters 123

if mystr[0:3] == '123':
    condition1
else:
    condition2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question