Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
convert string number to real number - ValueError: invalid literal
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Kam-Hung Soh, post: 3554078"] There doesn't seem to be a way to describe how specific columns should be processed in the csv module. You could define a conversion function that "guesses" the best conversion, for example: def str2num(datum): try: return int(datum) except: try: return float(datum) except: return datum for row in csv.reader(file(r'Transaction.csv')): [str2num(cell) for cell in row] ['Date', 'No.', 'Description', 'Debit', 'Credit'] ['3/17/2006', 5678, 'ELECTRONIC PAYMENT', '', 11.449999999999999] ['3/04/2007', 5678, 'THE HOME DEPOT 263 SomeCity FL', '', 25.399999999999999] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
convert string number to real number - ValueError: invalid literal
Top