Line indexing in Python

Discussion in 'Python' started by seafoid, Dec 18, 2009.

  1. seafoid

    seafoid Guest

    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.
    seafoid, Dec 18, 2009
    #1
    1. Advertising

  2. On Dec 18, 3:42 pm, seafoid <> 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.
    Richard Thomas, Dec 18, 2009
    #2
    1. Advertising

  3. seafoid

    seafoid Guest

    Thanks for that Richard and Steve.

    I have another question.

    fname = raw_input('Please enter the name of the file: ')

    # create file objects

    blah = open(fname, 'r')
    a = open('rubbish', 'w')

    for line in blah:
    if line.startswith("0"):
    a.write(line)
    elif line.endswith("0"):
    lists_a = line.strip().split()
    print lists_a
    elif line.startswith("0"):
    lists_b = line.strip().split()
    print lists_b

    Essentially, I wish to take input from a file and based on the location of
    zero, assign lines to lists.

    Any suggestions?

    Seafoid.

    --
    View this message in context: http://old.nabble.com/Line-indexing-in-Python-tp26845253p26845949.html
    Sent from the Python - python-list mailing list archive at Nabble.com.
    seafoid, Dec 18, 2009
    #3
  4. seafoid

    Lie Ryan Guest

    On 12/19/2009 3:27 AM, seafoid wrote:
    >
    > Thanks for that Richard and Steve.
    >
    > I have another question.


    What's the question?

    > fname = raw_input('Please enter the name of the file: ')
    >
    > # create file objects
    >
    > blah = open(fname, 'r')
    > a = open('rubbish', 'w')
    >
    > for line in blah:
    > if line.startswith("0"):
    > a.write(line)
    > elif line.endswith("0"):
    > lists_a = line.strip().split()
    > print lists_a


    The following block is a dead code; the block will never be executed
    since if line.startswith("0") is true, the control will fall to the
    a.write(line) block and this block is skipped.
    > elif line.startswith("0"):
    > lists_b = line.strip().split()
    > print lists_b
    >
    > Essentially, I wish to take input from a file and based on the location of
    > zero, assign lines to lists.
    >
    > Any suggestions?
    >
    > Seafoid.
    >
    Lie Ryan, Dec 18, 2009
    #4
  5. seafoid

    seafoid Guest

    Thanks for that Lie.

    I had to have a think about what you meant when you referred to control
    going to a.write(line).

    Have you any suggestions how I may render this code undead or should I scrap
    it and create something new?

    My confusion and ineptitude is perhaps explained by my being a biologist :-(

    Thanks,
    Seafoid.
    --
    View this message in context: http://old.nabble.com/Line-indexing-in-Python-tp26845253p26846854.html
    Sent from the Python - python-list mailing list archive at Nabble.com.
    seafoid, Dec 18, 2009
    #5
  6. On 18/12/09, seafoid () wrote:
    > Have you any suggestions how I may render this code undead or should I scrap
    > it and create something new?


    It might be easier for us to help you if you give us an example of your
    input file and a clearer description of what you are trying to do with
    the output from your programme.

    --
    Rory Campbell-Lange


    Campbell-Lange Workshop
    www.campbell-lange.net
    0207 6311 555
    3 Tottenham Street London W1T 2AF
    Registered in England No. 04551928
    Rory Campbell-Lange, Dec 18, 2009
    #6
  7. seafoid

    Lie Ryan Guest

    On 12/19/2009 4:33 AM, seafoid wrote:
    >
    > Thanks for that Lie.
    >
    > I had to have a think about what you meant when you referred to control
    > going to a.write(line).


    and if-elif-elif-... chain is executed sequentially and when a match is
    found, the rest of the chain is skipped. Your code:

    if line.startswith("0"):
    # BLOCK 1 #
    elif line.endswith("0"):
    # BLOCK 2 #
    elif line.startswith("0"):
    # BLOCK 3 #

    BLOCK 3 never gets executed, since if line.startswith("0") is true, your
    BLOCK 1 is executed and the rest of the if-elif chain is skipped.


    > Have you any suggestions how I may render this code undead or should I scrap
    > it and create something new?


    I still don't get what you want to do with the code, but to make it not
    dead you can either:

    for line in blah:
    if line.startswith("0"):
    a.write(line)
    lists_b = line.strip().split()
    print lists_b
    elif line.endswith("0"):
    lists_a = line.strip().split()
    print lists_a

    or this:

    for line in blah:
    if line.startswith("0"):
    a.write(line)
    if line.endswith("0"):
    lists_a = line.strip().split()
    print lists_a
    elif line.startswith("0"):
    lists_b = line.strip().split()
    print lists_b

    depending on which one seems more readable to you.

    > My confusion and ineptitude is perhaps explained by my being a biologist :-(
    >
    > Thanks,
    > Seafoid.
    Lie Ryan, Dec 18, 2009
    #7
  8. seafoid

    r0g Guest

    seafoid 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.



    If you want to know the index number of an item in a sequence you are
    looping through (whether it be a file of lines or a list of characters,
    whatever) use enumerate...

    >>> for index, value in enumerate("ABCD"):

    print index, value
    ....
    0 A
    1 B
    2 C
    3 D


    If you want to extract an index number from the first part of of a given
    line use split( split_character, maximum_splits_to_do ) and then angle
    brackets to reference the first part (index 0)...


    >>> a = "20 GOTO 10"
    >>> int( a.split(' ',1)[0] )

    20


    Cheers,


    Roger.
    r0g, Dec 21, 2009
    #8
  9. seafoid

    Steve Holden Guest

    r0g wrote:
    > seafoid 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.

    >
    >
    > If you want to know the index number of an item in a sequence you are
    > looping through (whether it be a file of lines or a list of characters,
    > whatever) use enumerate...
    >
    >>>> for index, value in enumerate("ABCD"):

    > print index, value
    > ...
    > 0 A
    > 1 B
    > 2 C
    > 3 D
    >
    >
    > If you want to extract an index number from the first part of of a given
    > line use split( split_character, maximum_splits_to_do ) and then angle
    > brackets to reference the first part (index 0)...
    >
    >
    >>>> a = "20 GOTO 10"
    >>>> int( a.split(' ',1)[0] )

    > 20
    >

    <nit>
    those are brackets, not angle brackets
    </nit>

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
    Holden Web LLC http://www.holdenweb.com/
    UPCOMING EVENTS: http://holdenweb.eventbrite.com/
    Steve Holden, Dec 22, 2009
    #9
  10. seafoid

    Lie Ryan Guest

    On 12/22/2009 11:25 PM, Steve Holden wrote:
    >> >
    >> > If you want to extract an index number from the first part of of a given
    >> > line use split( split_character, maximum_splits_to_do ) and then angle
    >> > brackets to reference the first part (index 0)...
    >> >
    >> >
    >>>>> >>>> a = "20 GOTO 10"
    >>>>> >>>> int( a.split(' ',1)[0] )
    >> > 20
    >> >

    > <nit>
    > those are brackets, not angle brackets
    > </nit>
    >


    <double_nit>
    those [] are square brackets, not angle brackets
    </double_nit>
    Lie Ryan, Dec 22, 2009
    #10
  11. seafoid

    r0g Guest

    Steve Holden wrote:
    > r0g wrote:
    >> seafoid 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.

    >>
    >> If you want to know the index number of an item in a sequence you are
    >> looping through (whether it be a file of lines or a list of characters,
    >> whatever) use enumerate...
    >>
    >>>>> for index, value in enumerate("ABCD"):

    >> print index, value
    >> ...
    >> 0 A
    >> 1 B
    >> 2 C
    >> 3 D
    >>
    >>
    >> If you want to extract an index number from the first part of of a given
    >> line use split( split_character, maximum_splits_to_do ) and then angle
    >> brackets to reference the first part (index 0)...
    >>
    >>
    >>>>> a = "20 GOTO 10"
    >>>>> int( a.split(' ',1)[0] )

    >> 20
    >>

    > <nit>
    > those are brackets, not angle brackets
    > </nit>
    >
    > regards
    > Steve



    <nit++>
    They're actually square brackets, "brackets" on its own is more commonly
    used as a synonym for parentheses (round brackets). But yes, I did get
    that wrong in the above ;)
    </nit++>

    Cheers,

    Roger :)
    r0g, Dec 22, 2009
    #11
  12. seafoid

    MRAB Guest

    Lie Ryan wrote:
    > On 12/22/2009 11:25 PM, Steve Holden wrote:
    >>> >
    >>> > If you want to extract an index number from the first part of of a
    >>> given
    >>> > line use split( split_character, maximum_splits_to_do ) and then
    >>> angle
    >>> > brackets to reference the first part (index 0)...
    >>> >
    >>> >
    >>>>>> >>>> a = "20 GOTO 10"
    >>>>>> >>>> int( a.split(' ',1)[0] )
    >>> > 20
    >>> >

    >> <nit>
    >> those are brackets, not angle brackets
    >> </nit>
    >>

    >
    > <double_nit>
    > those [] are square brackets, not angle brackets
    > </double_nit>


    <triple_nit>
    [] are brackets, () are parentheses, {} are braces
    </triple_nit>
    MRAB, Dec 23, 2009
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. C
    Replies:
    0
    Views:
    493
  2. Hugo
    Replies:
    10
    Views:
    1,299
    Matt Humphrey
    Oct 18, 2004
  3. Emin
    Replies:
    4
    Views:
    407
    Paul McGuire
    Jan 12, 2007
  4. Skybuck Flying
    Replies:
    30
    Views:
    1,095
    Bill Reid
    Sep 19, 2011
  5. C
    Replies:
    3
    Views:
    216
    Manohar Kamath [MVP]
    Oct 17, 2003
Loading...

Share This Page