[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1824
  • Last Modified:

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
0
Ricky Ng
Asked:
Ricky Ng
  • 2
  • 2
  • 2
  • +1
2 Solutions
 
Pasha KravtsovCommented:
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
 
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 KravtsovCommented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now