Read text file and parse in python

Read text file and parse in python

@supremed14, you can also try the below code to prepare the list of dictionaries after reading the file.

data.txt

As white spaces are there in text file. strip() method defined on strings will solve this problem.

Date, Day, Sect, 1, 2, 3

1, Sun, 1-1, 123, 345, 678

2, Mon, 2-2, 234, 585, 282

3, Tue, 2-2, 231, 232, 686

Source code:

Here you do not need to worry about closing the file. It will be taken care by Python.

import json
my_list = [];

with open(data.txt) as f:
    lines = f.readlines() # list containing lines of file
    columns = [] # To store column names

    i = 1
    for line in lines:
        line = line.strip() # remove leading/trailing white spaces
        if line:
            if i == 1:
                columns = [item.strip() for item in line.split(,)]
                i = i + 1
            else:
                d = {} # dictionary to store file data (each line)
                data = [item.strip() for item in line.split(,)]
                for index, elem in enumerate(data):
                    d[columns[index]] = data[index]

                my_list.append(d) # append dictionary to list

# pretty printing list of dictionaries
print(json.dumps(my_list, indent=4))

Output:

[
    {
        Date: 1,
        Day: Sun,
        Sect: 1-1,
        1: 123,
        2: 345,
        3: 678
    },
    {
        Date: 2,
        Day: Mon,
        Sect: 2-2,
        1: 234,
        2: 585,
        3: 282
    },
    {
        Date: 3,
        Day: Tue,
        Sect: 2-2,
        1: 231,
        2: 232,
        3: 686
    }
]

Using pandas this is pretty easy:

Input:

$cat test.txt
Date, Day, Sect, 1, 2, 3
1, Sun, 1-1, 123, 345, 678
2, Mon, 2-2, 234, 585, 282
3, Tue, 2-2, 231, 232, 686

Operations:

import pandas as pd
df = pd.read_csv(test.txt, skipinitialspace=True)
df.loc[df[Sect] == 2-2].to_dict(orient=records)

Output:

[{1: 234, 2: 585, 3: 282, Date: 2, Day: Mon, Sect: 2-2},
 {1: 231, 2: 232, 3: 686, Date: 3, Day: Tue, Sect: 2-2}]

Read text file and parse in python

If your .txt file is in the CSV format:

Date, Day, Sect, 1, 2, 3

1, Sun, 1-1, 123, 345, 678

2, Mon, 2-2, 234, 585, 282

3, Tue, 2-2, 231, 232, 686

You can use the csv library:

from csv import reader
from pprint import pprint

result = []
with open(file.txt) as in_file:

    # create a csv reader object
    csv_reader = reader(in_file)

    # extract headers
    headers = [x.strip() for x in next(csv_reader)]

    # go over each line 
    for line in csv_reader:

        # if line is not empty
        if line:

            # create dict for line
            d = dict(zip(headers, map(str.strip, line)))

            # append dict if it matches your condition
            if d[Sect] == 2-2:
                result.append(d)

pprint(result)

Which gives the following list:

[{1: 234, 2: 585, 3: 282, Date: 2, Day: Mon, Sect: 2-2},
 {1: 231, 2: 232, 3: 686, Date: 3, Day: Tue, Sect: 2-2}]

Leave a Reply

Your email address will not be published. Required fields are marked *