python csv processing

Asked 2 years ago, Updated 2 years ago, 22 views

Prerequisites/What you want to achieve

I'm thinking of reading the following csv files and organizing the data.

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

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

2016 12 23 55 9.6 South-southeast
2016 12 23 6 9.8 southeast
2016 12 23 7 10.6 East
2016 12 23 8 10.4 Northeast
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 Northwest
2016 1223 156.2 Northwest
2016 12 23 166.5 North-West
2016 12 23 17 6.3 North-West
2016 12 23 18 6.3 North
2016 12 23 19 5.4 Southwest
2016 12 23 20 3.9 West-Southwest
2016 12 23 214 South-West
2016 12 23 22 4.6 South
2016 12 23 23 24 South


How do I do this? ➀Squares the wind speed value.

次 Next, we would like to make angle correction, so depending on the wind direction, cos を is multiplied by the square value of the wind speed
ex) For the above data
·The wind direction is divided into 16 directions, so the interval between the angles of the adjacent directions is 22.5°
With the west 22.5° for west-northwest and west-southwest, 45° for northwest and southwest, 67.5°

for north-northwest and south-southwest

·Based on the angle based on the west, multiply the square value of the wind speed by cosθ each
(10.5)^2×cos(0)+(9.1)^2×cos(0)+
(8.1)^2×cos(22.5)+(7.8)^2×cos(22.5)+(6.8)^2×cos(22.5)+
(6.3)^2×cos(45)+(6.2)^2×cos(45)+
(6.5)^2×cos(67.5)+(6.3)^2×cos(67.5)+
(5.4)^2×cos(45)+
(3.9)^2×cos(22.5)+
(4)^2×cos (67.5)

➂ Add all values of の that satisfy a specific wind direction (in this case 'south-southwest', 'south-southwest', 'west-southwest', 'west-northwest', 'north-northwest', 'north-northwest').

I would like to do this.

Problems/Error Messages you are experiencing

Squares the value of the wind speed that currently satisfies a specific wind direction and adds it together
It moved to the point where
➁Like this, I am worried that I cannot apply cos (22.5) to certain wind directions and cos (45) to certain wind directions.

Source Codes Affected

The current program is shown below.
I'm sorry for the long sentence.
So I'm thinking of moving it in this way right now.
Here's how it works:


    import csv
    import path

    cos0=math.cos(math.radians(0))
    cos225 = path.cos(math.radians (22.5))
    cos45=math.cos(math.radians(45))
    cos675 = path.cos(math.radians (67.5))

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

    Direction = ['South-South-West', 'South-West', 'West-West', 'West-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])
# I'm stumbling from here on out.


After this
➀ All colE squared

D
in seven directions of Direction Multiply colE (squared) with west orientation by cos0
Multiply colE (squared) whose azimuth is west-southwest and west-northwest by cos 225
COLE (squared) whose azimuth is southwest and northwest multiplied by cos45
Multiply colE (squared) in south-southwest and north-northwest by cos675

2 Add the squared value to the corrected value

I'm thinking that I can do it in that way.

python

2022-09-29 22:32

3 Answers

If you try to avoid making any changes to the source code of the questionnaire, you will see the following:
@akiraejiri Assume that the full-width blank in the csv data you pointed out is corrected to a half-width blank.

  • Use **2 to square the wind speed
  • For values based on wind direction, prepare a list of coefficients corresponding to the wind direction list, obtain the index of the matching wind direction string, and import data from the same index position in the coefficient list
  • Instead of adding all of the above after calculation, we will add each time

The modified and added lines and their vicinity are described in comments.

import csv
import path

cos0=math.cos(math.radians(0))
cos225 = path.cos(math.radians (22.5))
cos45=math.cos(math.radians(45))
cos675 = path.cos(math.radians (67.5))

f = open("test.csv", "r")
# Change the csv delimiter to a blank character in the line below and skip any extra spaces
reader=csv.reader(f, delimiter=', skipinitialspace=True)
next(reader);next(reader);next(reader);next(reader);next(reader)

Direction = ['South-South-West', 'South-West', 'West-West', 'West-West', 'North-West', 'North-West', 'North-West']
# List of coefficient data corresponding to the above direction
DFactor=[cos675, cos45, cos225, cos0, cos225, cos45, cos675]

sum = 0.0 # Initialize to 0.0 since floating point
For row in reader:
    if(not row[4]) or(not row[5]): # Skip line without wind speed or wind direction data
        continue
    if not row[5] in Direction: Skip wind speed and direction data not included in #Direction
        continue
    colE=float(row[4])
    # add the square of the wind speed × the corresponding coefficient of the wind direction to the sum
    sum+=(colE**2)*DFactor [Direction.index(row[5])]

print(sum)# Display total values, adjust to decimal places x digits if necessary, etc.


2022-09-29 22:32

The following csv file was used as a confusing expression.
I should have pasted the actual file and communicated it well.

Based on the respondent's code, I moved the actual csv file.
I got an error saying list index out of range

 if(not row[4]) or(not row[5]): # Skip line without wind speed or wind direction data

It stopped here.

Compared to the code I used to do similar things before

import csv
import path

cos0=math.cos(math.radians(0))
cos225 = path.cos(math.radians (22.5))
cos45=math.cos(math.radians(45))
cos675 = path.cos(math.radians (67.5))

f=open("Hamo 23.csv", "r")
reader=csv.reader(f)
next(reader);next(reader);next(reader);next(reader);next(reader)

Direction = ['South-South-West', 'South-West', 'West-West', 'West-West', 'North-West', 'North-West', 'North-West']
# List of coefficient data corresponding to the above direction
DFactor=[cos675, cos45, cos225, cos0, cos225, cos45, cos675]

sum = 0.0 # Initialize to 0.0 since floating point
For row in reader:
    if not row[4].strip():# Skip Empty Row
        continue
    if not row[5] in Direction: Skip wind speed and direction data not included in #Direction
         continue
    colE=float(row[4])
    # add the square of the wind speed × the corresponding coefficient of the wind direction to the sum
    sum+=(colE**2)*DFactor [Direction.index(row[5])]

print(sum)# Display total values, adjust to decimal places x digits if necessary, etc.

I changed it a little and it worked.

The reason was that the following csv data I posted was not good.
Sorry.

The code above worked.
I'm sorry for the inconvenience.
Thank you for your answers and comments.


2022-09-29 22:32

This answer was out of the question.
The Error 1 and Error 2 in the original answer are not errors.

I misunderstood the following csv file in the question as the following csv file.

, separated by for row in reader: is a list of items, so the size len(row) is not 1.

Below is the original answer.

I tried to run the source code, but an error occurred and # I stumbled from now on.

was not reached.

Error No. 1

 if not row[4].strip():# Skip Empty Row

row was a list of sizes 1, and row[4] failed.
If you simply want to skip an empty line, I think the following code is fine.

if not row:# Skip empty lines

Error No.2

colE=float(row[4])

Error The error occurred for the same reason as error number 1.
If you want to divide it into columns by blank or tab code (multiple), I think you can use the following code.

import re
row=re.split('[\t]+', row[0])
colE=float(row[4])

Other
It may be just a transcription error, but there was a blank space between the 5th column and the direction.

2016 12 23 7 10.6 East
2016 12 23 8 10.4 Northeast
2016 12 23 9 10.5 West         ↑ This is a full-width blank


2022-09-29 22:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.