Line indexing in Python

S

seafoid

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

Richard Thomas

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.

'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.
 
S

seafoid

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

Lie Ryan

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

seafoid

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

Rory Campbell-Lange

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
(e-mail address removed)

Campbell-Lange Workshop
www.campbell-lange.net
0207 6311 555
3 Tottenham Street London W1T 2AF
Registered in England No. 04551928
 
L

Lie Ryan

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

r0g

seafoid said:
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...
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.
 
S

Steve Holden

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

Lie Ryan

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>
 
R

r0g

Steve said:
r0g said:
seafoid said:
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 :)
 
M

MRAB

Lie said:
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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top