I'm attempting to import text from a flat file and to convert it to float values within a single line. I've seen this post which has the same error, but I haven't found which characters are invalid in my input file. Or do I have a syntax error?
Import as a string an print the result:
data = np.loadtxt(file, delimiter='\t', dtype=str)
print(data[0:2])
...
[["b'Time'" "b'Percent'"]
["b'99'" "b'0.067'"]]
Attempt to import as float:
# Import data as floats and skip the first row: data_float
data_float = np.loadtxt(data, delimiter='\t', dtype=float, skiprows=1)
It throws the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
data_float = np.loadtxt(data, delimiter='\t', dtype=float, skiprows=1)
File "<stdin>", line 848, in loadtxt
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "<stdin>", line 848, in <listcomp>
items = [conv(val) for (conv, val) in zip(converters, vals)]
ValueError: could not convert string to float: b'["b\'99\'" "b\'0.067\'"]'
By the way, I've also seen this post which explains the b
character, but I don't think that's the issue.
An additional troubleshooting step as suggested by the first answer:
data = np.loadtxt(file, delimiter="\tb'", dtype=str)
Returns:
array(["b'Time\\tPercent'", "b'99\\t0.067'", "b'99\\t0.133'",
"b'99\\t0.067'", "b'99\\t0'", "b'99\\t0'", "b'0\\t0.5'",
"b'0\\t0.467'", "b'0\\t0.857'", "b'0\\t0.5'", "b'0\\t0.357'",
"b'0\\t0.533'", "b'5\\t0.467'", "b'5\\t0.467'", "b'5\\t0.125'",
"b'5\\t0.4'", "b'5\\t0.214'", "b'5\\t0.4'", "b'10\\t0.067'",
"b'10\\t0.067'", "b'10\\t0.333'", "b'10\\t0.333'", "b'10\\t0.133'",
"b'10\\t0.133'", "b'15\\t0.267'", "b'15\\t0.286'", "b'15\\t0.333'",
"b'15\\t0.214'", "b'15\\t0'", "b'15\\t0'", "b'20\\t0.267'",
"b'20\\t0.2'", "b'20\\t0.267'", "b'20\\t0.437'", "b'20\\t0.077'",
"b'20\\t0.067'", "b'25\\t0.133'", "b'25\\t0.267'", "b'25\\t0.412'",
"b'25\\t0'", "b'25\\t0.067'", "b'25\\t0.133'", "b'30\\t0'",
"b'30\\t0.071'", "b'30\\t0'", "b'30\\t0.067'", "b'30\\t0.067'",
"b'30\\t0.133'"],
dtype='<U16')