Digitize csv files in python

Asked 2 years ago, Updated 2 years ago, 51 views

Downloaded time: 11/11/2019 16:04:33                  

                staying overnight at a temple
Wind direction (m/s) per hour (month/day)

2016 12 23 5 9.6 West
2016 12 23 6 9.8 West
2016 12 23 7 10.6 West
2016 12 23 8 10.4 West
2016 12 23 9 10.5 West
2016 12 23 10 9.1 West
2016 12 23 11 8.1 West-northwest
2016 12 23 12 7.8 West-northwest
2016 12 23 13 6.8 West-northwest
2016 12 23 14 6.3 West-northwest
2016 1223 156.2 West-West
2016 12 23 166.5 West-northwest
2016 12 23 17 6.3 Northwest
2016 12 23 18 6.3 West-northwest
2016 1223 195.4 West-northwest
2016 12 23 20 3.9 West-northwest
2016 12 23 214 West-northwest
2016 12 23 22 4.6 Northwest
2016 12 23 23 24 Northwest

The method of summarization is to target the wind direction that satisfies the five directions of west, west-northwest, northwest, north-northwest, and north-northwest in 16 directions. We would like to square the wind values of 8 m/s or higher and add them together.

Currently, the wind speed is 8 m/s or higher in the if statement, and the target wind direction is divided into cases
I stumble when I add that value by square.

Also, all wind speeds are squared, and the value of 8^2=64 or higher is divided according to the wind direction of the target
I think it would be better to add the square value of the wind speed that meets that.

The current code is listed below.
Please check it out.

import csv

f=open("Terayuki 12.csv", "r")
reader=csv.reader(f)
next(reader);next(reader);next(reader);next(reader);next(reader)

Direction = ['West', 'West-West', 'North-West', 'North-West', 'North-West', 'North-West']

For row in reader:
  if not row[4].strip():# Skip Empty Row
    continue
  colE=float(row[4])
  if(colE>=8) and (row[5] in Direction):

python csv

2022-09-30 10:31

1 Answers

Use the ** or the built-in power function for power calculations.
You can also use math.power, but note that unlike the above, you can treat arguments as float and make exceptions for operations outside the real range

>>>power(8,2)
64

>>>9.6**2
92.16

>>import path
>>math.power(8,2)# argument and return value are float
64.0

>>>power(-1,0.5)#Square root with negative bottom
(6.1232339957366e-17+1j)
>>math.power (-1,0.5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError:math domain error

>>>power(1,1j)# exponent is a complex number
(1+0j)
>>math.power (1,1j)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't convert complex to float

The if statement for "Square and add up the wind speed that meets the requirements" is written under the correct conditions, so based on the above, I think you can achieve your goal by adding a variable to add up the squared value of colE.

import csv

f=open("Terayuki 12.csv", "r")
reader=csv.reader(f)
next(reader);next(reader);next(reader);next(reader);next(reader)

Direction = ['West', 'West-West', 'North-West', 'North-West', 'North-West', 'North-West']

sum = 0
For row in reader:
  if not row[4].strip():# Skip Empty Row
    continue
  colE=float(row[4])
  if(colE>=8) and (row[5] in Direction):
    sum+=power(colE,2)

print(f' wind speed of 8 m/s or more squared plus {sum:.2f}.')


2022-09-30 10:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.