Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
22
Task/CSV-data-manipulation/Python/csv-data-manipulation-2.py
Normal file
22
Task/CSV-data-manipulation/Python/csv-data-manipulation-2.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import csv
|
||||
from pathlib import Path
|
||||
from tempfile import NamedTemporaryFile
|
||||
|
||||
filepath = Path('data.csv')
|
||||
temp_file = NamedTemporaryFile('w',
|
||||
newline='',
|
||||
delete=False)
|
||||
|
||||
with filepath.open() as csv_file, temp_file:
|
||||
reader = csv.reader(csv_file)
|
||||
writer = csv.writer(temp_file)
|
||||
|
||||
header = next(reader)
|
||||
writer.writerow(header + ['SUM'])
|
||||
|
||||
for row in reader:
|
||||
row_sum = sum(map(int, row))
|
||||
writer.writerow(row + [row_sum])
|
||||
|
||||
temp_file_path = Path(temp_file.name)
|
||||
temp_file_path.replace(filepath)
|
||||
Loading…
Add table
Add a link
Reference in a new issue