0

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

1
  • Set "list_1" to an empty list and "append" each list in "content" to "list_1" in the for-loop. So you get a list of lists. Commented Sep 18, 2020 at 0:47

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

Comments

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.