right now I have Python 3 code that takes a column of data within a CSV file, delimits the phrases in each cell into individual words based on spaces, then exports the data back into a new CSV file.
What I am wondering about is if there is a way to tell python to only apply the formatting code to a specific column with a particular header?
Here is what my source data looks like
Keyword Source Number
Lions Tigers Bears US 3
Dogs Zebra Canada 5
Sharks Guppies US 2
and here is my code which delimits the phrases in each cell into individual words based on a space
with open(b'C:\Users\jk\Desktop\helloworld.csv', 'r') as datafile:
data = []
for row in datafile:
data.extend(item.strip() for item in row.split())
with open('test.csv', 'w') as a_file:
for result in data:
result = ''.join(result)
a_file.write(result + '\n')
print(result)
so that the source data becomes
Keywords Source Number
Lions US 3
Tigers
Bears
Dogs Canada 5
etc
In this case, I only need all of this code to apply to the one column with the heading Keyword. Ideally, what I am trying to do is also extend the data found in the "Source" and "Number" to these newly created rows (Lions US 3 -- Tigers US 3 -- Bears US 3 etc) but I haven't really figured out that part yet!
I've been poking around the forum for awhile trying to find an answer and I know you can tell python to read the first line of the CSV file where the headers are placed (headers = file.readline()) but beyond that I am lost. Would this be an easier task using the CSV reader?
Cstands for Character; both comma and tabs are common. I'll assume you have comma-separated data then; your sample data doesn't give much of a hint.