John_U262D
John_U262D

Reputation: 31

Python import txt formatting

I have an Excel file with a list of numbers and I saved it as a .txt file and then went to say:

open_file = open('list_of_numbers.txt','r')

for number in open_file:
    number = int(number)
    while x < 20000:
        if (x > number):
            print number
        x = x + 100
        y = y + 100

And I received this error message:

ValueError: invalid literal for int() with base 10: '2100.00\r\n'

How can I strip the ' and the \r\n'?

My ultimate goal is to create another column next to the column of numbers and, if the number is 145 for example,

145, '100-199'
167, '100-199'
1167, '1100-1199'

that sort of output.

Upvotes: 0

Views: 1424

Answers (3)

John Machin
John Machin

Reputation: 82924

To address your immediate problem, go with the answer by @Felix Kling.

If you are interested in your FUTURE problems, please read on.

(1) That \r is not part of the problem IN THIS PARTICULAR CASE, but is intriguing: Are you creating the file on Windows and reading it on Linux/OSX/etc? If so, you should open your text file with "rU" (universal newlines), so that the input line on Python has only the \n.

(2) In any case, it's a very good idea to do line = line.rstrip('\n') ... otherwise, depending on how you split up the lines, you may end up with your last field containing an unwanted \n.

(3) You may prefer to use xlrd to read from an Excel file directly -- this saves all sorts of hassles. [Dis]claimer: I'm the author of xlrd.

Upvotes: 1

Felix Kling
Felix Kling

Reputation: 816364

Let's put it as an answer. The problem is not \r\n. The problem is that you try to parse string that contains a float value as an integer. See (no line feed, new line characters):

>>> int("2100.00")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '2100.00'

(as you can see, the quotation marks ' are not part of the value, they just indicate that you are dealing with a string)

whereas

>>> int("2100\r\n")
2100

The documentation says:

If the argument is a string, it must contain a possibly signed decimal number representable as a Python integer, possibly embedded in whitespace.

where the Python integer literal definition can be found here.

Solution:

Use float:

>>> float("2100.00\r\n")
2100.0

then you can convert it to an integer if you want to (also consider round):

>>> int(float("2100.00\r\n"))
2100

Converting a float value to integer works (from the documentation):

Conversion of floating point numbers to integers truncates (towards zero).

Upvotes: 4

ChrisD
ChrisD

Reputation: 3518

Try this:

number = int(number.strip(string.whitespace + "'"))

You will need to add import string to the beginning of the your script. See also: http://docs.python.org/library/stdtypes.html#str.strip

Upvotes: -1

Related Questions