Issue
import csv
with open('data1.txt', 'r') as f:
fread = csv.reader(f, delimiter='\t')
output = []
for line in fread:
output.append(line)
data_values = str(output[1:17]) #skip the first line and grab relevant data from lines 1 to 16
print(data_values)
usable_data_values = float(data_values)
Right now I'm trying to convert a .txt file with two columns of data into an array with two columns of data. At this point I've extracted the data and have this:
[['0.0000', '1.06E+05'], ['0.0831', '93240'], ['0.1465', '1.67E+05'],
['0.2587', '1.54E+05'], ['0.4828', '1.19E+05'], ['0.7448', '1.17E+05'],
['0.9817', '1.10E+05'], ['1.2563', '1.11E+05'], ['1.4926', '74388'], ['1.7299', '83291'],
['1.9915', '66435'], ['3.0011', '35407'], ['4.0109', '21125'], ['5.0090', '20450'],
['5.9943', '15798'], ['7.0028', '4785.2']]
I'm trying to get that data into something usable (I think I need to get rid of the commas but I'm new to Python and don't even know how to do this). Any help would be appreciated on getting these numbers into a usable form for operations(multiply, add, divide, etc.)!
Solution
I believe this is the best you can do. The commas are fine, they just indicate separate elements of a python list. Notice the quotation marks disappear to indicate you are no longer dealing with text strings.
lst = [['0.0000', '1.06E+05'], ['0.0831', '93240'], ['0.1465', '1.67E+05'],
['0.2587', '1.54E+05'], ['0.4828', '1.19E+05'], ['0.7448', '1.17E+05'],
['0.9817', '1.10E+05'], ['1.2563', '1.11E+05'], ['1.4926', '74388'], ['1.7299', '83291'],
['1.9915', '66435'], ['3.0011', '35407'], ['4.0109', '21125'], ['5.0090', '20450'],
['5.9943', '15798'], ['7.0028', '4785.2']]
[list(map(float, i)) for i in lst]
# [[0.0, 106000.0],
# [0.0831, 93240.0],
# [0.1465, 167000.0],
# [0.2587, 154000.0],
# [0.4828, 119000.0],
# [0.7448, 117000.0],
# [0.9817, 110000.0],
# [1.2563, 111000.0],
# [1.4926, 74388.0],
# [1.7299, 83291.0],
# [1.9915, 66435.0],
# [3.0011, 35407.0],
# [4.0109, 21125.0],
# [5.009, 20450.0],
# [5.9943, 15798.0],
# [7.0028, 4785.2]]
Answered By - jpp
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.