Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
16 events
when toggle format what by license comment
Aug 18, 2014 at 15:17 comment added user3682157 absolutely fantastic, thank you so much! Now I just need to do some reverse engineering on my end to see exactly how you did it!
Aug 18, 2014 at 7:42 comment added Martijn Pieters @user3682157: use writer.writerows(dict(row, Keyword=w) for w in words) to write the rows; remove the row['Keyword'] = words[0] and writer.writerow(row) lines. The dict(row, Keyword=w) line creates a copy of the row dictionary with the Keyword column replaced with one of the words.
Aug 18, 2014 at 5:17 comment added user3682157 So the python code is storing each row within a dictionary that can be called using the column header -- that is awesome! You have been a huge help especially to a newbie like myself. I'm currently reading up on dictionaries and that they are mutable. With that in mind, the next challenge I'm facing is extending the data down to each new row created when the cells within the keyword column are delimited (i.e. tigers US 3; bears US 3, etc). Any idea what I should be looking up to tackle this question? It would be a huge help :) Thank you!
Aug 17, 2014 at 23:10 comment added Martijn Pieters I'm not sure I follow you but I think the answer is yes. The header is read then used as names for each column. Python produces the dictionary for each row on demand as you read the rows.
Aug 17, 2014 at 23:08 comment added user3682157 Fantastic work Martijn, thank you. The only question I have is regarding this statement you made: "The DictReader() will read the first row from your file and use it as the keys for the dictionaries produced for each row". Does this mean that each column within the document I upload can be referenced by just the header? How is Python storing each of these dictionaries? Sorry for all the questions, I am just trying to learn!
Aug 17, 2014 at 23:03 vote accept user3682157
Aug 17, 2014 at 22:53 history edited Martijn Pieters CC BY-SA 3.0
added 2 characters in body; added 7 characters in body
Aug 17, 2014 at 22:47 history edited Martijn Pieters CC BY-SA 3.0
edited body
Aug 17, 2014 at 22:42 history edited Martijn Pieters CC BY-SA 3.0
added 105 characters in body; added 66 characters in body; added 20 characters in body
Aug 17, 2014 at 21:40 comment added user3682157 Hi Martijn, thank you for the response and the commentary! Very useful especially with this dictreader! The only issue is that my original code doesn't just remove the white spaces within the keyword row (i.e. lionstigersbears). What it does is delimits each of those "phrases" so that it becomes a stacked column where each cell only has one word! I have updated my original post to make this clearer
Aug 16, 2014 at 17:02 comment added Martijn Pieters @PadraicCunningham: no, the point was to remove spaces from that column. The column is split on whitespace, then re-joined without.
Aug 16, 2014 at 17:01 comment added Padraic Cunningham should LionsTigersBears not be Lions Tigers Bears?
Aug 16, 2014 at 15:19 comment added Martijn Pieters @JonClements: I was trying to 'reuse' a concept from the OP code; simply showing how to apply the same transformation to just one column. Since the original code uses ''.join() I think it is fair to assume that that is the goal but for just one column.
Aug 16, 2014 at 15:18 comment added Jon Clements row['Keyword'] = row['Keyword'].replace(' ','') looks better to me than the split/join. I have a feeling the OP could be after an output row of 3 columns though for the Lions Tigers Bears (but not sure)
Aug 16, 2014 at 15:17 history edited Martijn Pieters CC BY-SA 3.0
added 677 characters in body
Aug 16, 2014 at 15:08 history answered Martijn Pieters CC BY-SA 3.0