I'm learning Python during coding class at school. When processing data in CSV file among Excel, I try to process it using repetitive statements I'm asking because it didn't work out.crying Using the CSV module, I know up to there, but I don't know after that.
If you have a CSV file like this,
1 2 3
4 5 6
7 8 9
. . .
A = first row B = second row C = 3rd row (A+B)/C => is calculated continuously and stored in a new csv file For example, line 1 = (1+2)/3
Thank you.
python
Hello. You sent me an inquiry via email, but I'm posting an answer on the hash code. To help people who have similar concerns when they visit this article later!
The questioner could read the csv file and process the data. I didn't know how to write data to the csv file.
Python provides a csv module , which is useful for reading and writing csv files. The csv.writer
and writerow
functions allow you to write data to the csv file.
I changed the author's code a little bit. Please refer to the following code.
from numpy import *
import csv
data = loadtxt("test1.csv", delimiter=',', dtype=float)
A = data[:, 0]
B = data[:, 1]
C = data[:, 2]
D = len(A)
with open('output.csv', 'w+') as csvfile:
writer = csv.writer(csvfile)
for i in range(D):
E = (A[i] + B[i]) / C[i]
writer.writerow([A,B,C,E])
© 2024 OneMinuteCode. All rights reserved.