Home > Software design >  How to add duplicates in python text file?
How to add duplicates in python text file?

Time:03-02

I have this text file which is ppe.txt where it contains this

GH,FS,25
KH,GL,35
GH,FS,35

how do I identify GH as they appeared twice and add the values so it becomes

GH,FS,60
KH,GL,35

is it possible to do this?

CodePudding user response:

Input

GH,FS,25
KH,GL,35
GH,FS,35

Read the text file and merge duplicates.

key_values = {}
with open("myFile.txt", "r") as f:
    lines = [line.split(",") for line in f]
    for line in lines:
        key = f"{line[0]},{line[1]}"
        value = int(line[-1])
        if key not in key_values:
            key_values[key] = value
        key_values[key]  = value

Export to .csv

import csv

a_file = open("sample.csv", "w")
writer = csv.writer(a_file)
for key, value in key_values.items():
    writer.writerow([key, value])
a_file.close()

Output:

GH,FS,60
KH,GL,35

CodePudding user response:

Open your file and add "a,b,c" as first line. These will be the column names.

import pandas as pd

df = pd.read_csv("your_csv.csv")

results = {}
for _, row in df.iterrows():
   results = results.get(row["a"], 0)   row["c"]

print(results)
// {'GH': 60, 'KH': 35}

This basically reads the csv file and inserts the values into a dictionary.

  • Related