Home > Software engineering >  I want to delete A column from B column. how can i do it?
I want to delete A column from B column. how can i do it?

Time:09-25

I want to delete nm.csv from xc.csv

for eg A - B = result.

I have attached the two nm.csv and xc.csv files below

https://docs.google.com/spreadsheets/d/1VTzUR-cfto9EYEnL8gtt5HFIAA-YgzjJoON1XSMwL2M/edit?usp=sharing

https://docs.google.com/spreadsheets/d/14YUmW4KetembqsugwNqshaGjbSidExMVCN4pLeTf5JQ/edit?usp=sharing

Here is my script I wrote and I dont know what am I doing wrong?

import os

with open('/Users/robinmalhotra/Desktop/nm.csv') as nm:
    first = nm.read().splitlines()

with open('/Users/robinmalhotra/Desktop/xc.csv') as xc:
    second = xc.read().splitlines()

bigb = set(first) - set(second)

with open('nm.csv', 'w') as f1:
    out = '\n'.join(list(bigb))
    f1.write(out)

I am getting result as nm.csv but I want to delete nm - xc = result. I am getting A - B = B anyone can tell what am I doing wrong?

CodePudding user response:

This small modification extracts only the first column from the two files:

import os

with open('/Users/robinmalhotra/Desktop/nm.csv') as nm:
    first = [ln.strip().split(',')[0] for ln in nm.readlines()]

with open('/Users/robinmalhotra/Desktop/xc.csv') as xc:
    second = [ln..strip().split(',')[0] for ln in xc.readlines()]

bigb = set(first) - set(second)

with open('nm.csv', 'w') as f1:
    out = '\n'.join(list(bigb))
    f1.write(out)

CodePudding user response:

The csv library should help you read the files cleanly. The source files seem to have a different number of columns, so the set.difference method won't work because they aren't alike.

  • Related