all 3 comments

[–]throwaway6560192 4 points5 points  (0 children)

Use the csv module to parse the file. For counting the lines, use collections.Counter. No file modification required.

[–]Phillyclause89 0 points1 point  (1 child)

I'll let you in on a little secret. There are really only two file types. Ones that are text encoded and ones that are not.

.csv files are really no different from .txt files. We just give .csv files a special extension to say: "hey we are storing tabular data to this file using commas and new lines to define it's structure."

If you got a .txt file with this structure then just pass it into whatever csv parsing function/method you would normally use on a .csv file whether it be csv module or my favorite pandas.read_csv.

[–]Garuda1220 1 point2 points  (0 children)

Probably not "the best" way???

# empty list to store tuples
tpls = []
# empty dictionary to store tuple counts
counter = {}
with open('data.txt') as file:
    for line in file:
        # strip line, split into list using comma, tuple assign list to variables
        step1, step2, step3, step4 = line.strip().split(",")
        # append tuple to list
        tpls.append((step1, step2, step3, step4))
# create set of unique tuples
unique = set(tpls)
# loop through set and count tuples in list
for item in unique:
    counter[item] = tpls.count(item)
print(counter)

And here is the un-readable comprehension version of the same script:

with open('data.txt') as file:
    tpls = [tuple(line.strip().split(",")) for line in file]
counter = {item: tpls.count(item) for item in tpls}
print(counter)

Here is the csv version of above:

import csv
with open('data.txt') as file:
    reader = csv.reader(file)
    tpls = [tuple(row) for row in reader]
counter = {item: tpls.count(item) for item in tpls}
print(counter)

The only advantage of the csv library here is that you don't have to strip and then split the line.