Home > Enterprise >  Create table from txt file
Create table from txt file

Time:01-27

I have a .txt file that looks just like a table with multiple rows and even more columns, this is just an example:

FRU          LPP      BOARD                      BO  QU  CT (TX/dBm)  VSP (RyL)   RX (dBm) UEs/RLs
RR           B_20     RB7                        A  11  -     -       1,31 18    2.9    -/-    
RT           B_21     RB7                        B  11  -     -       1,09 3     2.1    20/-    

I'm interested in extracting data from a specific column. It feels like i've tried everything with Pandas read_csv function but i have not been able to get results, it seems like an easy task but i'm really stuck here

Columns_from_txt = VA.partition('\n')[0].split()[0:-2] #extracting top row to be my columnames
Row_from_txt = VA.split('\n')[2:-2] #extracting all rows  
df = pd.read_csv(Row_from_txt, names=Columns_from_txt)
print(df['BOARD'])

This is just the latest example from all that i've tried, i dont know what else to do than to reach out to you guys. Thanks!

CodePudding user response:

Update, now with creating the dataframe from string:

from io import StringIO
import pandas as pd

text = StringIO("""FRU          LPP      BOARD                      BO  QU  CT (TX/dBm)  VSP (RyL)   RX (dBm) UEs/RLs
RR           B_20     RB7                        A  11  -     -       1,31 18    2.9    -/-    
RT           B_21     RB7                        B  11  -     -       1,09 3     2.1    20/-    """)

df = pd.read_csv(text, delimiter=r"\s ")

Output:

    FRU     LPP     BOARD   BO  QU  CT  (TX/dBm)    VSP     (RyL)   RX  (dBm)   UEs/RLs
0   RR      B_20    RB7     A   11  -   -           1,31    18      2.9 -/-     NaN
1   RT      B_21    RB7     B   11  -   -           1,09    3       2.1 20/-    NaN

and print(df["BOARD"]):

0    RB7
1    RB7
Name: BOARD, dtype: object
  •  Tags:  
  • Related