On Dec 18, 3:42 pm, seafoid <fitzp...@tcd.ie> wrote:
> Hi Guys,
>
> When python reads in a file, can lines be referred to via an index?
>
> Example:
>
> for line in file:
>      if line[0] == '0':
>          a.write(line)
>
> This works, however, I am unsure if line[0] refers only to the first line or
> the first character in all lines.
>
> Is there an easy way to refer to a line with the first character being a
> single letter that you know?
>
> Thanks in advance,
> Seafoid.
> --
> View this message in 
> context:http://old.nabble.com/Line-indexing-in-Python-tp26845253p26845253.html
> Sent from the Python - python-list mailing list archive at Nabble.com.

'for line in file' goes through the lines of the file. 'line[0]' is
then the first character of that line. You'll need to index them
manually, for which you should use a dictionary:

index = {}
for line in file:
    index[line[0]] = line
a.write(index['0'])

Richard.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to