0

I'm reading in a text file using the command line via input() in Python 3, and lines in the file that appear numerical are being automatically treated as integers, causing errors. My understanding is that input() is not supposed to do any evaluation, and should treat these as strings. What is causing this behavior and how do I prevent it?

EDIT: For a cleaner example, if my code instead simply reads

cases = input()
print(type(cases))

My input file reads:

100
4 2

And I type into cmd:

script.py < input_file.txt > output_file.txt

My output file will contain

<type 'int'>
ZWarner
  • 9
  • 2

1 Answers1

0

@Patrick Artner is almost certainly correct. Your code should perform as expected in Python 3, but in Python 2 you'd get the result you're seeing instead.

The Python 2 equivalent of Python 3's input() is raw_input(), which converts the input to a string as you expect.

EDIT: By the way, never ever use input() in Python 2. It evalutes the user's input, and therefore is unsafe. (What if the input were os.system("<some really horrible command>")?) That's why Python 3 replaced it.

B. Shefter
  • 877
  • 7
  • 19