What is the Python code to delete first column in a .csv file?

What is the Python code to delete first column in a .csv file?

The columns after the first column are dynamic.

The data looks like this

112323, 12, 23
1433332, 44
222232, 77,22,34
544545, 21,34,45,13,45
335353, 12

I want the  result to look like this:

12, 23
44
77,22,34
21,34,45,13,45
12

Thanks
Ricky NgAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pasha KravtsovSupport EngineerCommented:
Try this:
import csv
with open("you_csv_file.csv","rb") as csvFile:
    csvReader = csv.reader( csvFile )
    with open("your_finished_csv_file.csv","wb") as csvResult:
        csvWrite = csv.writer( csvResult )
        for row in csvWrite:
            del row[0]
            csvWrite.writerow(row)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ricky NgAuthor Commented:
Hi Pasha,

So this code will remove the first column and save the file with the original file name?

Thanks
0
Pasha KravtsovSupport EngineerCommented:
Yes, and no. I'd create a backup just in case I made an error since I did this on the fly but I'm confident it should work
Just when it saves the new file (currently named "your_finished_csv_file.csv") just rename it to your original
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

peprCommented:
Alternatively, the
            del row[0]
            csvWrite.writerow(row)

Open in new window

can be replaced by
            csvWrite.writerow(row[1:])

Open in new window

that is slicing the row -- from the element at the position 1 (means second element) to the end of the row.

(But all points should go to Pasha. ;)

A side note: I suggest to do some things rather mechanically, with some personal convention. My experience is that it is a good idea to name all needed variables that cannot be confused the same way. Sometimes, it help to avoid bugs -- like in this exact case. That is I would write the code like this:
import csv
fname_in = 'you_csv_file.csv'
fname_out = 'your_finished_csv_file.csv'
with open(fname_in, 'rb') as fin, open(fname_out, 'wb') as fout:
    reader = csv.reader(fin)
    writer = csv.writer(fout)
    for row in reader:
        writer.writerow(row[1:])

Open in new window

0
Ricky NgAuthor Commented:
Pasha,

Got this error:

  File "del_col_test.py", line 6, in <module>
    for row in csvWrite:
TypeError: '_csv.writer' object is not iterable
0
Walter RitzelSenior Software EngineerCommented:
just replace csvWriter by csvReader in the row:
for row in csvWrite:

Open in new window

It should be:
for row in csvReader:

Open in new window

0
Walter RitzelSenior Software EngineerCommented:
The solution for this question should be: https:#a40249283, and assisted answer (which have fixed one bug on the original answer) is: https:#a40250625
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.