python - separating column for only one time by separator in Pandas -


i have data

abc:def how met ter:kpefe hi 

i want column this

a       b         c abc    def     how met ter    kpefe   hi 

i want separate 3 column using

data = pd.read_csv(input_file, sep=" |:", header = none, names=["a", "b", "c"]) 

which giving many column beside a, b , c

my stab @ uses csv library read input lists since needs (heavily) sanitized before can neatly put dataframe want.

# python 3.5 import pandas pd  import csv   col1 = [] col2 = [] col3 = []  open('path/to/the/file.txt', newline='') txt:     reader = csv.reader(txt)     row in reader:         # rid of brackets , ' on both ends of string         str_row = str(row)[1:-1].strip("'")         # first column's element         split1 = str_row.split(':')         col1.append(split1[0])         # second column's element         split2 = split1[1].split(' ')         col2.append(split2[0])         # join after second column's element         # third column's element         split3 = ' '.join([v v in split2[1:]])         col3.append(split3)  df = pd.dataframe({'a':col1, 'b':col2, 'c':col3}) print(df) 

produces

              b              c   0  abc    def  how met   1  ter  kpefe   hi 

like mentioned, i'm making naive assumption of data in structured in way. if don't want manually put in column names (for scalability) can use nifty trick (which automatically put integers column names) build dataframe (referencing this thread):

# gives same desired output df = pd.dataframe(list(map(list, zip(col1, col2, col3)))) 

Comments