read numbers from file and covert to integer

  • Thread starter Øystein Western
  • Start date
Ø

Øystein Western

I got a file with a lot blocks of numbers that are strings. I'd like to read
all
this numbers and convert them to numbers for futher compareation. How can I
convert all this numbers to integer? Do I have to put all numbers into a
list?

regards
frengky


--
Organisation nr: 983063349
Frengky, Olsokveien 65,1727 Sarpsborg, Norway
Tel: +47 92611725
Fax: +47 69152017
Email: (e-mail address removed)
Web: www.frengky.no
 
P

Peter Hansen

Øystein Western said:
I got a file with a lot blocks of numbers that are strings. I'd like to read
all
this numbers and convert them to numbers for futher compareation. How can I
convert all this numbers to integer? Do I have to put all numbers into a
list?

It would help immensely if you could post some kind of
example of the sorts of data you are talking about,
perhaps a hex representation of the bytes (if it's
a binary file). Otherwise we are just guessing how
you are using the terms "block", "number", "string",
and even "integer".

-Peter
 
B

beliavsky

If you have a file "ints.txt" with contents

10 20
30
40 50 60

You could read integers into a list with

ivec = []
for text in open("ints.txt","r"):
words = text.split()
for x in words:
ivec.append(int(x))
print ivec

If you know you want to read two integers into variables a,b from a
line you could write

a,b = int(words[0]),int(words[1])

Excuse me, but if your question is really so elementary that this post
answered it, you should probably read a book or tutorial.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top