I am having a problem with the data I have collected. When I write the data to a file it is shown like this.

(231.2314,32434.344,134.2342,345.345,1234.45)

(2323.567,32.6754,12323.8776,12323.575,233.5673)

now when I read data and put it in a list it looks like this.

data = ['(231.2314,32434.344,134.2342,345.345,1234.45)
\n','(2323.567,32.6754,12323.8776,12323.575,233.5673)\n']

is there any way i can just retrieve the floats from the file. When I try to convert to float I get an error about '(' is not a float. Can someone point me into the right direction. I have attached the data. Thanks.

Edited 6 Years Ago by ish13: n/a

Attachments
(4.377528190612793, 0.95460051298141479, 0.0, 0.0, 0.0, 0.0, 2.4891839027404785, 6.4788622856140137, 9.1542816162109375, 10.640993118286133, 9.4116535186767578, 5.4576315879821777, 2.1075818538665771, 5.9804105758666992, 9.9998235702514648, 17.566879272460938, 22.773008346557617, 24.290367126464844, 25.559301376342773, 26.570148468017578, 26.530355453491211, 24.595857620239258, 23.040992736816406, 21.636260986328125, 14.895512580871582, 5.7990102767944336, 2.0596811771392822, 0.55083096027374268, 3.5647168159484863, 2.7824246883392334, 2.5293488502502441, 2.9357614517211914, 1.8516901731491089, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.9749982357025146, 4.6345658302307129, 5.4318490028381348, 4.332395076751709, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.1657286882400513, 4.9291706085205078, 4.2029008865356445, 0.98258674144744873, 0.0, 0.49498024582862854, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.2954151630401611, 3.975787878036499, 5.0127954483032227, 4.3974933624267578, 5.1960783004760742, 6.2857532501220703, 6.9268183708190918, 6.6808629035949707, 6.599644660949707, 6.105553150177002, 1.0305488109588623, 0.0, 0.0, 0.0, 0.0, 0.51007157564163208, 6.1416950225830078, 8.2269182205200195, 7.5084433555603027, 3.0538859367370605, 0.0, 0.0, 0.0, 0.0, 0.0, 1.9555066823959351, 6.7777643203735352, 6.9097952842712402, 7.9582347869873047, 8.6534843444824219, 8.7505130767822266, 9.2742748260498047, 8.8994626998901367, 8.1872825622558594, 15.02519702911377, 21.645986557006836, 29.130744934082031, 38.345172882080078, 43.559955596923828, 44.36297607421875, 43.911651611328125, 43.695785522460938, 43.220870971679688, 42.860252380371094, 42.767055511474609, 42.859378814697266, 42.924003601074219, 43.024295806884766, 41.072216033935547, 33.661205291748047, 25.642292022705078, 31.17413330078125, 33.918704986572266, 34.924747467041016, 34.112709045410156, 34.335918426513672, 33.824886322021484, 33.479583740234375, 33.034374237060547, 32.761260986328125, 31.855442047119141, 25.977863311767578, 17.877548217773438, 16.117887496948242, 21.009992599487305, 21.2083740234375, 21.229679107666016, 20.511655807495117, 19.794490814208984, 18.891302108764648, 17.555322647094727, 16.438379287719727, 15.05325984954834, 9.0852537155151367, 4.5496292114257812, 0.0, 0.94872230291366577, 6.1809415817260742, 6.3743934631347656, 6.6340503692626953, 7.7656164169311523, 7.1089000701904297, 7.0322637557983398, 7.3586640357971191, 7.7096529006958008, 8.0553369522094727, 4.396723747253418, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 7.0579404830932617, 14.336895942687988, 18.001110076904297, 18.293302536010742, 18.706003189086914, 18.717735290527344, 17.905227661132812, 18.459449768066406, 17.432550430297852, 11.125414848327637, 11.250707626342773, 17.256736755371094, 18.987091064453125, 14.474138259887695, 4.0327425003051758, 0.0, 2.0145211219787598, 6.1486606597900391, 8.123321533203125, 7.2998161315917969, 6.0320529937744141, 5.420957088470459, 0.24794070422649384, 2.5522322654724121, 7.894871711730957, 12.674936294555664, 18.896987915039062, 22.72389030456543, 23.800970077514648, 23.378913879394531, 23.277090072631836, 23.739810943603516, 23.890447616577148, 23.186059951782227, 23.145271301269531, 15.893016815185547, 8.4749126434326172, 4.3848223686218262, 2.0593750476837158, 3.9488966464996338, 5.0148763656616211, 6.1466464996337891, 6.6395516395568848, 5.3549389839172363, 2.3847663402557373, 1.1083877086639404, 1.2164630889892578, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.75112742185592651, 0.0, 1.1925915479660034, 1.0545679330825806, 1.3094034194946289, 0.52316689491271973, 0.91200548410415649, 0.0, 0.0, 2.3233535289764404, 11.693458557128906, 16.630218505859375, 19.984251022338867, 19.020200729370117, 15.913283348083496, 15.403774261474609, 14.434388160705566, 14.285286903381348, 15.853943824768066, 15.604782104492188, 14.839963912963867, 10.787397384643555)

(13.143330574035645, 21.987405776977539, 28.361394882202148, 30.487279891967773, 30.339174270629883, 30.14666748046875, 29.873952865600586, 29.787965774536133, 29.464391708374023, 28.992176055908203, 28.702642440795898, 27.000238418579102, 20.18950080871582, 9.1465864181518555, 0.0, 0.0, 0.0, 0.0, 0.13621717691421509, 1.2412046194076538, 1.8618952035903931, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 6.9516463279724121, 13.774548530578613, 21.258398056030273, 26.387887954711914, 27.217247009277344, 27.410371780395508, 27.22907829284668, 27.247390747070312, 27.188777923583984, 26.811494827270508, 25.655509948730469, 20.411148071289062, 13.269503593444824, 5.8229665756225586, 0.0, 0.0, 0.0, 4.4010028839111328, 6.7027316093444824, 6.181769847869873, 4.1141304969787598, 0.0, 0.0, 0.0, 0.0, 0.24533993005752563, 6.1719756126403809, 10.941356658935547, 15.547703742980957, 19.498510360717773, 19.704767227172852, 19.360902786254883, 18.983757019042969, 18.813779830932617, 18.862634658813477, 18.219505310058594, 16.050125122070312, 10.111244201660156, 3.6113901138305664, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3.1459801197052002, 9.4893999099731445, 15.383016586303711, 17.839498519897461, 24.903800964355469, 27.933225631713867, 28.014499664306641, 28.27824592590332, 28.075309753417969, 28.296945571899414, 28.137166976928711, 27.733686447143555, 27.183521270751953, 25.329732894897461, 19.507360458374023, 13.667203903198242, 6.3680362701416016, 1.8010873794555664, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.489666223526001, 4.2438459396362305, 5.9471659660339355, 7.5097136497497559, 7.2005448341369629, 6.6085524559020996, 3.7049582004547119, 2.9417159557342529, 4.2694978713989258, 1.9005378484725952, 0.0, 0.0, 0.0, 0.0, 0.0, 4.2436518669128418, 6.9705801010131836, 6.8016648292541504, 3.0672938823699951, 0.0, 0.0, 0.0, 0.0, 0.0, 2.211552619934082, 6.5876712799072266, 7.0694046020507812, 8.421905517578125, 8.9752645492553711, 9.2101545333862305, 8.949127197265625, 9.0644454956054688, 8.881321907043457, 10.418767929077148, 18.47755241394043, 27.195608139038086, 35.9586181640625, 43.356845855712891, 45.340614318847656, 45.080158233642578, 44.859653472900391, 44.704242706298828, 44.676185607910156, 44.729454040527344, 45.015476226806641, 45.350639343261719, 45.598209381103516, 44.3062744140625, 37.514183044433594, 33.923679351806641, 38.345390319824219, 39.325523376464844, 39.048194885253906, 38.977596282958984, 39.272201538085938, 39.405208587646484, 39.512382507324219, 39.586872100830078, 39.616371154785156, 37.826038360595703, 31.354154586791992, 26.012418746948242, 21.348642349243164, 25.741382598876953, 30.788511276245117, 32.493404388427734, 32.574993133544922, 32.916549682617188, 32.669876098632812, 32.734973907470703, 32.453517913818359, 32.476310729980469, 28.393976211547852, 21.426929473876953, 16.037666320800781, 8.1226539611816406, 0.86160892248153687, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.4534444808959961, 8.5363054275512695, 8.5021934509277344, 10.731196403503418, 16.327264785766602, 17.923662185668945, 18.062717437744141, 18.70020866394043, 18.361072540283203, 17.794034957885742, 16.210012435913086, 10.794870376586914, 2.031597375869751, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0648937225341797, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 4.3197512626647949)

(3.5439486503601074, 0.0, 0.0, 0.0, 0.0, 0.0, 5.3890371322631836, 9.8610162734985352, 9.1258811950683594, 9.2959680557250977, 10.283190727233887, 5.9223175048828125, 0.0, 6.3929853439331055, 12.213179588317871, 21.099273681640625, 28.130117416381836, 29.163473129272461, 29.843893051147461, 29.762241363525391, 29.863922119140625, 29.524148941040039, 28.92570686340332, 23.151760101318359, 15.562368392944336, 10.456056594848633, 2.404071569442749, 4.2705636024475098, 8.5000104904174805, 9.0698308944702148, 7.8331422805786133, 7.0973572731018066, 5.1996655464172363, 0.85254430770874023, 0.0, 0.0, 0.0, 1.5667997598648071, 7.3683962821960449, 12.593905448913574, 21.967403411865234, 26.388383865356445, 27.127235412597656, 27.441953659057617, 28.040548324584961, 28.117105484008789, 28.717676162719727, 28.94395637512207, 28.612043380737305, 27.907329559326172, 20.763420104980469, 15.02680492401123, 11.424881935119629, 6.1149125099182129, 0.49047762155532837, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 5.3118281364440918, 10.830447196960449, 14.257186889648438, 19.899005889892578, 27.050226211547852, 27.749502182006836, 28.000638961791992, 27.745080947875977, 27.04803466796875, 26.906021118164062, 26.278873443603516, 25.884498596191406, 25.121957778930664, 20.689676284790039, 11.315496444702148, 6.1157431602478027, 0.58842653036117554, 0.0, 0.0, 0.0, 0.0, 3.2325799465179443, 5.1648240089416504, 6.082909107208252, 7.3308987617492676, 6.6791486740112305, 2.4148802757263184, 0.30534139275550842, 8.0260410308837891, 13.094656944274902, 20.494537353515625, 26.202333450317383, 26.810184478759766, 26.995662689208984, 26.861587524414062, 26.740650177001953, 25.831798553466797, 24.617801666259766, 17.569915771484375, 8.5728092193603516, 1.9442259073257446, 0.0, 1.3038702011108398, 5.054354190826416, 4.2273068428039551, 3.8026747703552246, 4.224942684173584, 1.7539231777191162, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3.2665460109710693, 6.9608345031738281, 11.332054138183594, 14.322939872741699, 15.101718902587891, 15.426131248474121, 15.43186092376709, 15.033510208129883, 15.476777076721191, 14.911191940307617, 13.359259605407715, 7.5896224975585938, 0.86071372032165527, 0.0, 0.87878185510635376, 0.65832656621932983, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.4860960245132446, 10.78502368927002, 14.15198802947998, 18.071012496948242, 23.945571899414062, 28.303766250610352, 28.952915191650391, 29.360130310058594, 29.431825637817383, 29.44597053527832, 29.803197860717773, 29.437356948852539, 29.377246856689453, 26.223997116088867, 20.072004318237305, 15.199555397033691, 9.5339927673339844, 5.2687268257141113, 0.0, 0.0, 0.0, 0.0, 0.0,

Here is one way you can approach your problem ...

import pprint

test_data = """\
(231.2314,32434.344,134.2342,345.345,1234.45)
(2323.567,32.6754,12323.8776,12323.575,233.5673)
"""
# write the test data file out
fname = "atest.txt"
fout = open(fname, "w")
fout.write(test_data)
fout.close()

# read the test data file back in
fin = open(fname, "r")
data_list1 = []
data_list2 = []
for line in fin:
    # strip whitespace
    line = line.strip()
    # strip '('
    line = line.strip('(')
    # srip ')'
    line = line.strip(')')
    #convert string to list of floats at the comma
    data_float = [float(x) for x in line.split(',')]
    # create a list of line sublists
    data_list1.append(data_float)
    # create one list of all data items
    data_list2.extend(data_float)

pprint.pprint( data_list1 )

print('-'*30)

pprint.pprint( data_list2 )

""" result (typical binary rep of floats) >>>
[[231.23140000000001,
  32434.344000000001,
  134.23419999999999,
  345.34500000000003,
  1234.45],
 [2323.567,
  32.675400000000003,
  12323.8776,
  12323.575000000001,
  233.56729999999999]]
------------------------------
[231.23140000000001,
 32434.344000000001,
 134.23419999999999,
 345.34500000000003,
 1234.45,
 2323.567,
 32.675400000000003,
 12323.8776,
 12323.575000000001,
 233.56729999999999]
"""

Some numbers look a little strange at first, but most computer languages handle floating point numbers as binary numbers which can introduce a very tiny error with some numbers.

Thanks a lot. Worked perfect and solved my problem. Thanks again

After some practise with Python idioms, you could maybe prefer this list comprehension:

test_data = """\
(231.2314,32434.344,134.2342,345.345,1234.45)
(2323.567,32.6754,12323.8776,12323.575,233.5673)
"""
data = [ tuple( float(string_data)
                for string_data in eval(data_tuple))
         for data_tuple in test_data.splitlines()
         if data_tuple.startswith('(')]

for data_tuple in data:
    print type(data_tuple[0]),data_tuple
This question has already been answered. Start a new discussion instead.