Skip to content Skip to sidebar Skip to footer

Python Csv Filter Pairs And Nonpairs

The text file I have is like below: C1,D1,,,,,,,, Layer_00 , 3.46ms,Layer_01 , 3.40ms,Layer_02 , 3.56ms,Layer_03 , 3.49ms,Layer_04 , 3.44ms Layer_05 , 3.45ms,Layer_06 , 3.44ms,Laye

Solution 1:

This should work, but you can't declare the value as float if it will also hold strings ("C1","C2"):

with open('text.txt', 'r') as file:
    pairs = re.findall('(Layer_\d+|C\d)\s*,\s*(\d+\.\d+|D\d)[ms]*', file.read())
pairs = [(k, v) for k,v in pairs]
df = pd.DataFrame(pairs)

Post a Comment for "Python Csv Filter Pairs And Nonpairs"