'Updating filedata and copying it to another file
I have such code which is incorrect:
import csv
with open("pastimes.csv", "r") as my_file:
my_file_reader = csv.reader(my_file)
for row in my_file_reader:
if row[0] == 'Person':
row[0] = 'Name'
elif row[1] == 'Favorite pastime':
row[2] = 'Type of Pastime'
if row[1].lower().find('fighting') >= 0:
row.append('Combat')
else:
row.append('Other')
with open("categorized pastimes.csv", "w") as my_file2:
my_file_writer = csv.writer(my_file2)
my_file_writer.writerows(row)
My pastimes data is:
Person,Favorite pastime
Fezzik,Fighting
Westley,Winning
Inigo Montoya,Sword fighting
Buttercup,Complaining
I need to get such result
['Name', 'Favorite pastime', 'Type of Pastime']
['Fezzik', 'Fighting', 'Combat']
['Westley', 'Winning', 'Other']
['Inigo Montoya', 'Sword fighting', 'Combat']
['Buttercup', 'Complaining', 'Other']
And write it to file categorized pastimes.csv
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
