Skip to content Skip to sidebar Skip to footer

Pandas Read "delimited" File

Hi, I have such .txt file, with the first column represent index, which is followed by three columns inside a pair of '()' representing x, y and z coordinates. I want to load the

Solution 1:

It is possible to write your own parser. Something like:

Code:

defparse_my_file(filename):
    withopen(filename) as f:
        for line in f:
            yield [x.strip(',()')
                   for x in re.split(r'\s+', line.strip())[:4]]

Test Code:

df = pd.DataFrame(parse_my_file('file1'))
print(df)

Results:

    0       1       2  3
0  g1     -16       0  0
1  gr      10       0  0
2  D1  -6.858  2.7432  0
3  D2  -2.286  2.7432  0

This data file was created when I typed in your first four rows.

Solution 2:

You can use regex pattern as seperator of CSV.

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html

like this.

import pandas as pd

df = pd.read_csv('Initial_Coordinate.txt', sep=r'[()]', header=None)
print(df)

However, rather than creating complex delimiters, it is better to fix it as a simple delimiter and then read it with pandas.

thx

Post a Comment for "Pandas Read "delimited" File"