0

The input CSV file is below.

Elements,FileID,Loadcase,Step,Layer,CornerID,E-Strain components IP:E11
41,2,1,0,SectionPoint 1,0,0
41,2,1,0,SectionPoint 1,5,0
41,2,1,0,SectionPoint 1,35,0

In this file, I want to remove the entire CornerID column and write a new CSV file.
Note: the script should work with header name and remove that column . Not like in the way of Column indices level deleting. because like this I have multiple files, in some files the column number will not be the same.

can anyone help me to get python code for this?

import csv

inputf = "D:\PyLe\csvinput\Report_E-Strain components IP_E11_2_1.csv" outfile = "D:\PyLe\csvinput\Report_E-Strain components IP_E11_2_1_new.csv"

import csv

    with open(inputf, newline='') as csvfile:
        outfile1 = open (outfile , 'w')
        reader = csv.DictReader(csvfile)

for row in reader:
      del (row['CornerID'])
      spamwriter = csv.writer(outfile1, delimiter=' ',
                        quotechar='|', quoting=csv.QUOTE_MINIMAL)
      spamwriter.writerow(row)
      print (type(row))

this code is reading the csv file and deleting that columnl. but its not writing out the new file with csv format.

2
  • How about df.drop(['CornerID'], axis=1, inplace=True) and then save it as a csv? Commented Jun 10, 2020 at 12:09
  • please send me the full code..:) Commented Jun 10, 2020 at 12:10

2 Answers 2

1

The easiest way to go is to use pandas. It is a common python module to handle tabular data.

import pandas as pd

csv = pd.read_csv("pathtoyourcsvfile.csv")
csv = csv.drop("CornerID", axis=1)
csv.to_csv("pathtoyournewcsvfile.csv")
Sign up to request clarification or add additional context in comments.

2 Comments

Please send the link where i can download pandas
1

Pandas installation

You can first read_csv, then drop the column and then save it as a csv using to_csv

import pandas as pd
df = pd.read_csv('your_CSV_file_name')
df.drop(['CornerID'], axis=1, inplace=True)
df.to_csv('path_or_buf_to_save_new_csv')

3 Comments

Could be. I am not aware of that. You should also remember that this is not a code writing platform. If you want a code that doesn't use Pandas, then I suggest you write your code and if any problem arises, you can post the problem here
import csv inputf = "D:\\PyLe\\csvinput\\Report_E-Strain components IP_E11_2_1.csv" outfile = "D:\\PyLe\\csvinput\\Report_E-Strain components IP_E11_2_1_new.csv" import csv with open(inputf, newline='') as csvfile: outfile1 = open (outfile , 'w') reader = csv.DictReader(csvfile) for row in reader: del (row['CornerID']) spamwriter = csv.writer(outfile1, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL) spamwriter.writerow(row) print (type(row)) inputf.close() outfile.close()
Through all of the blog posts on using this library for this purposes, none of them have the key last method call "df.to_csv('path')"

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.