473,387 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

List of string to float

2
Hi,

I'm reading from csv file using csv module and end-up with a list of string (while actual data is time stamp or float)
lst=['2010-02-15 18:22:08.918000', '2010-12-11 00:00:00.000000', '10740.0', '10740.0', '10750.0', '10745.0', '10741.2457045']

How can I convert list to date time and float using minimum line of fastest code. Fastest because csv size are upto 20 MB and have hundreds of them to read from.

Thanks,
Feb 22 '11 #1
2 2695
bvdet
2,851 Expert Mod 2GB
This is not intended to be the minimum line of ffastest code, but it's a start. How would you do it?

Expand|Select|Wrap|Line Numbers
  1. import time
  2.  
  3. def timeconv(s):
  4.     if "." in s:
  5.         s = s[:s.index(".")]
  6.     return time.strptime(s, "%Y-%m-%d %H:%M:%S")
  7.  
  8. def convdata(s):
  9.     for f in (float, timeconv):
  10.         try:
  11.             return f(s)
  12.         except:
  13.             pass
  14.     return s
  15.  
  16. data = ['2010-02-15 18:22:08.918000', '2010-12-11 00:00:00.000000', '10740.0', '10740.0', '10750.0', '10745.0', '10741.2457045']
  17.  
  18. data1 = [convdata(s) for s in data]
Feb 22 '11 #2
puns
2
At the moment I'm using exactly same approach as yours bvdet.

Thanks,
Feb 23 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: cjl | last post by:
Hey all: I want to convert strings (ex. '3', '32') to strings with left padded zeroes (ex. '003', '032'), so I tried this: string1 = '32' string2 = "%03s" % (string1) print string2 >32
6
by: malv | last post by:
Simple case: In this list, how to find all occurences of intervals of n adjacent indexes having at least one list-member with a value between given limits. Visualizing the list as a...
0
by: drewy2k12 | last post by:
Heres the story, I have to create a doubly linked list for class, and i have no clue on how to do it, i can barely create a single linked list. It has to have both a head and a tail pointer, and...
6
by: David M. Synck | last post by:
Hi all, I am fairly new to Python and trying to figure out a syntax error concerning lists and iteration through the same. What I am trying to do is sum a list of float values and store the sum...
15
by: thomas.mertes | last post by:
For a hash function I want to reinterpret the bits of a float expression as unsigned long. The normal cast (unsigned long) float_expression truncates the float to an (unsigned long) integer. ...
6
by: [david] | last post by:
returns poorly formatted values: '13.3' ''
1
by: malick | last post by:
Hello it developers, I'm using PHPExcelReader which I downloaded from sourceforge (http://sourceforge.net/projects/phpexcelreader) To read excel xls files in php. In the file reader.php (...
5
by: John | last post by:
Hi Suppose I want a header containing an image floated left and a list of horizontal links floated right. So I float the image left. I then do the following with the list: ul = float:...
10
by: lpinho | last post by:
Hi all, I have a class (named for the example myObject) that can be of several types (int, string, float, etc), instead of using a object to define it's type I used a generic. public class...
8
by: Ruben | last post by:
error: passing `const Weight' as `this' argument of `float Weight::wgt()' discards qualifiers seems to be some sort of standard error format that I'm not understanding. I have code that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.