hassan
hassan

Reputation: 133

how to access elements of string one by one

I have a text file with hex-number like:

000062240
000062A4B
000062244
000062245
000062D50
00006225E
00006A25F

I want to read it line-by-line that would be already done pretty straight forward. Now What I want to do after reading the first line. For example 000062240 split it in a element by element (or can be say "character by character")like

0 
0 
0
0
6 
2
2
4
0

here is the complete code but as when you use readline the return value is in string and it gives me the error unsupported operand type(s) for -: 'str' and 'int'

import time
# code to read the file one by one and remove white space result of the readline process
fo=open('test.txt','r')

for line in fo.readlines():
 recu =  line.strip('\n')
 print recu
 for element in recu:
  print recu[element-1]
  time.sleep(0.5)

Upvotes: 0

Views: 309

Answers (2)

Trelzevir
Trelzevir

Reputation: 765

Iterating through a string "character by character" is what happens when you use for element in recu:

So to generate the output described, you want to simply:

for element in recu:
    print element 

Outputs:

0
0
0
0
6
2
2
4
0

Upvotes: 0

nguyen thanh chinh
nguyen thanh chinh

Reputation: 161

your "element" is exactly what you need, just

print element

Upvotes: 1

Related Questions