Home > Mobile >  get number values from text in specific lines and get them into a list as numbers
get number values from text in specific lines and get them into a list as numbers

Time:07-14

I have a text file that each line of it is as follows:

    <vehicle id="tester.3" x="6936.49" y="10.20" angle="90.00" type="tester" speed="24.87" pos="6336.49" lane="longedge_3" slope="0.00"/>
    <vehicle id="tester.4" x="4388.72" y="7.00" angle="90.00" type="tester" speed="22.57" pos="3788.72" lane="longedge_2" slope="0.00"/>
    <vehicle id="tester.5" x="2075.13" y="13.40" angle="90.00" type="tester" speed="23.30" pos="1475.13" lane="longedge_4" slope="0.00"/>
    <vehicle id="tester.3" x="7836.49" y="14.20" angle="90.00" type="tester" speed="25.97" pos="6376.49" lane="longedge_3" slope="0.00"/>
                                           .
                                           .
                                           .

in each line containing tester.3 i need to extract the value of speed and get all these speeds in a list as numbers in order to use this as an input to make a violin plot

the python code i use is

import matplotlib.pyplot as plt
import numpy as np
import re

text = 'tester.3' 
new_list = []
idx = 0

with open('fcd.xml') as f:
a = f.readlines()
pattern = r'speed="([\d.] )"'

for line in a:
    if text in line:
        x=(re.findall(pattern, line))
        new_list.insert(idx, x)
        idx  = 1
print(new_list)




# Create a figure instance
fig = plt.figure()

# Create an axes instance
ax = fig.add_axes([0,0,1,1])

# Create the boxplot
bp = ax.violinplot(new_list)
plt.show()

The new list is printing like that

[['33.00'], ['32.61'], ['32.62'], ['31.48'], ['29.32'], ['25.86'],...]

and then error happens

      bp = ax.violinplot(new_list)
  File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\matplotlib\__init__.py", line 1412, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\matplotlib\axes\_axes.py", line 7942, 
in violinplot
    vpstats = cbook.violin_stats(dataset, _kde_method, points=points,
  File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\matplotlib\cbook\__init__.py", line 1485, in violin_stats
    min_val = np.min(x)
  File "<__array_function__ internals>", line 180, in amin
  File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\numpy\core\fromnumeric.py", line 2916, in amin
    return _wrapreduction(a, np.minimum, 'min', axis, None, out,
  File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\numpy\core\fromnumeric.py", line 86, in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
ValueError: zero-size array to reduction operation minimum which has no identity
    

I tried the violinplot algorithm with a simple list and it run so i suspect the problem is in the contains of my input list (new_list). They are strings instead of numbers or not? I am newby in programming so i dont know how to fix this anybody help?

CodePudding user response:

your list elements are string. you should convert it to float:

[float(r[0]) for r in your_list]
  • Related