Home > OS >  how to split data from text file and create separate columns at a specific position
how to split data from text file and create separate columns at a specific position

Time:05-14

it is a sample data

ao112   qwertyuiopasdfgh
ao12234 isbcbcobcwocbwowd
ao12    lscnldcnoodqhiod

I want to create a data frame using .txt file where the data frame needs to create separate columns in this example(2 columns) using this info col name | position code |1-7 blank |8 description|9- to end I need to create a dataframe columns by seprating from a specific position in the example case that is at blank position

tried using this code but unable to find what parameter should I use in sep

data=pd.read_csv('filepath',sep=' ',name=['code','description'])

CodePudding user response:

You can use pandas.read_fwf:

You can infer the format using colspecs='infer' or provide a full list of the ranges.

colspecslist of tuple (int, int) or ‘infer’. optional

A list of tuples giving the extents of the fixed-width fields of each line as half-open intervals (i.e., [from, to[ ). String value ‘infer’ can be used to instruct the parser to try detecting the column specifications from the first 100 rows of the data which are not being skipped via skiprows (default=’infer’).

(pd.read_fwf('filepath', colspecs='infer', header=None)
   .set_axis(['code','description'], axis=1)
)

Alternatively, use a regex separator with pandas.read_csv (if you have one or more spaces as delimiter):

pd.read_csv('filepath', sep='\s ', names=['code','description'])

output:

      code        description
0    ao112   qwertyuiopasdfgh
1  ao12234  isbcbcobcwocbwowd
2     ao12   lscnldcnoodqhiod
  • Related