Search code examples
python-3.xfile-read

How to read files in python


If my file content is (filename:test):

00001069: 04 33 c0 eb 42 53 8b 1e 6b 00 6a 04 6a 02 6a 00

00001078: 6a 02 68 00 00 00 70 68 38 30 00 10 ff 15 08 20

How do I read the file into content

ex :

df = pd.DataFrame(content,'test')

thank you


Solution

  • You can use pd.read_csv

    import pandas as pd
    data = pd.read_csv('test', sep=" ", header=None)
    print(data)
    

    Gives you,

              0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16
    0  00001069:  04  33  c0  eb  42  53  8b  1e  6b   0  6a   4  6a   2  6a   0
    1  00001078:  6a   2  68  00   0   0  70  68  38  30  00  10  ff  15  08  20
    

    You may label the columns too,

    data.columns = ["Code", "Elem 1", "Elem 2", "Elem 3", "Elem 4", "Elem 5", "Elem 6", "Elem 7", "Elem 8",
                    "Elem 9", "Elem 10", "Elem 11", "Elem 12", "Elem 13", "Elem 14", "Elem 15", "Elem 16"]
    print(data)
    

    Which gives you,

            Code Elem 1  Elem 2 Elem 3  ... Elem 13  Elem 14  Elem 15 Elem 16
    0  00001069:     04      33     c0  ...      6a        2       6a       0
    1  00001078:     6a       2     68  ...      ff       15       08      20