1

I am attempting to remove a duplicate column from a csv file in Python3. I am able to get the code to run without error...however when I attempt to print the number of rows processed I receive a blank response in codio.

The response to running comes back as 1001codio@sigma-portal:~/workspace$

Can anyone point out how I can fix this? Ideally I would like it to print how many columns were deleted.

import csv
import re
data = []

import csv

input_file = 'customerdata.csv'
output_file = 'outputcustomerdata.csv'
cols_to_remove = [11] # Column indexes to be removed (starts at 0)

cols_to_remove = sorted(cols_to_remove, reverse=True) # Reverse so we remove from the end first
row_count = 0 # Current amount of rows processed

with open(input_file, "r") as source:
   reader = csv.reader(source)
   with open(output_file, "wt") as result:
       writer = csv.writer(result)
       for row in reader:
           row_count += 1
           print('\r{0}'.format(row_count), end='') # Print rows processed
           for col_index in cols_to_remove:
               del row[col_index]
           writer.writerow(row)
2
  • check the length of reader Commented Aug 13, 2020 at 22:32
  • I recomend using pandas if you have to modify csv files Commented Aug 13, 2020 at 22:34

1 Answer 1

1

Try pandas

import pandas as pd
df = pd.read_csv('customerdata.csv')
df = df.drop('column_name', axis=1)

# save your file
df.to_csv('outputcustomerdata.csv', encoding='utf-8')

print(df)
Sign up to request clarification or add additional context in comments.

2 Comments

Codio does not recognize the pandas module :(
You have to install it. Try pip install pandas or pip3 install pandas on your terminal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.