0

Right now my code adds the text from the required rows from 2 different csv files into one list:

import csv  

res = []
with open('csvexample.csv') as f, open('csvexample1.csv') as a:
    reader=csv.reader(f) 
    reader1=csv.reader(a)
    next(reader)
    next(reader1)
    for row in zip(reader, reader1):
        res.extend([row[0][0], row[1][1]])  

print(res)  

Let's say csvexample.csv looks like this:
ID Text
1 'good morning'
2 'good afternoon'
3 'good evening'

and csvexample1.csv looks like this:
Day Month
14 'Feb'
21 'Mar'

31 'May'
10

The outcome is as follows:

['1', 'feb', '2', 'mar', '3', '']  

However, I want it to print out 'May' for the csvexample1.csv file was well, and just ignore the empty cells. What do I need to change about the current code to make sure that happens?

7
  • Did you write that? did you test it with your example csv's? Commented May 7, 2018 at 15:38
  • @wwii Parts of it, mostly puzzling existing code together and I got help with splitting the sentences etc. in words_cleaned. And yes I did test it with my example csv's. Commented May 7, 2018 at 15:45
  • 1
    Consider removing code that isn't needed for the example.... like the nltk stuff. And that code has several bugs (row[0][0] ??) that distract from the question. Post something that you have run yourself and that shows the problem. Otherwise you are sticking us with it. Commented May 7, 2018 at 15:46
  • @tdelaney This piece of code is part of a larger code where I add Tweets from two different companies to one list, and Tweets from their followers in another list in order to measure similarity between the two text which is why the nltk stuff is in there. I'm running it on the csv examples now because the csv files with the Tweets are very large. At first the csvexample1 did not have any blank cells which, so this part of the code produced the following outcome: ['1', 'feb', '2', 'mar', '3', 'may']. However, when checking to see if it still worked with empty cells, it did not print 'may'. Commented May 7, 2018 at 15:58
  • 1
    The why doesn't really mater for the question. You did a good job explaining the situation, its the code that's the problem. I wanted to copy your code to test my answer but you didn't make that easy. It has bugs and stuff I don't have installed on my machine. Make it easy! Commented May 7, 2018 at 16:06

1 Answer 1

1

Just add a generator that filters out empty rows

for row in zip((row for row in reader if row), 
    (row for row in reader1 if row)):
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.