TheMissingRobot
TheMissingRobot

Reputation: 3

Use csv.reader to read a file into a list but skip a specific column (Python)

Hello and thanks in advance for any help. I am new to Python and I've found and tried many solutions but I can't seem to get the correct output.

I have a csv file with several columns. I want to skip the field names/first row and then read the file into a list. I also want to skip a column resulting in something like: column 1, column 2, column 3, column 5... I want do this because I am merging two other csv files (that are converted to lists) and they have different structures.

Here's my original code before I discovered the csv files had different structures...

#convert input file1 to list
reader = csv.reader(file1,delimiter=',')
next(reader)    
list_1 = []
list_1 = list(reader)

I have tried:

reader = csv.reader(file1,delimiter=',')
next(reader)
included_cols = [0, 1, 2, 3, 5, 6, 7]

for row in reader:
    content = list(row[i] for i in included_cols)

list_1 = list(content)

But this doesn't output correctly down the line when I merge the three lists into a sorted list like so:

unsortedList = list_1 + list_2 + list_3

and then I create a sorted list:

sortedList = sorted(unsortedList,key=operator.itemgetter(0))

and try to output the file like this:

with open('output.csv','a') as result_file:
    wr = csv.writer(result_file, dialect='excel')
    wr.writerows(sortedList)

The resulting output: weird at the top

Upvotes: 0

Views: 158

Answers (1)

user2317421
user2317421

Reputation:

In general, I would use pandas instead. Say you have the CSV file called test.csv:

a,b,c,d
1,2,3,4
5,6,7,8

We can read it using pandas:

import itertools
import pandas as pd

df = pd.read_csv('test.csv', skiprows=[0], usecols=[0,1,3], header=None)
print(df)
   0  1  3
0  1  2  4
1  5  6  8

Then, you can generate the lists from rows as:

lists = df.values.tolist()

And finally into a single list:

merged = list(itertools.chain(*lists))
print(merged)
[1, 2, 4, 5, 6, 8]

Upvotes: 1

Related Questions