I want Python to load a blank-separated text file just like CSV.

Asked 2 years ago, Updated 2 years ago, 26 views

I would like to read the following txt files and organize the data.

Location information 
 Longitude: 139.535622 Latitude: 38.519794 inobservation 
 Longitude: 139.562500 Latitude: 38.500000 in MSM 
 Date UUV UV DIR TPR MSL 
 20180101-00:00    6.28130   -0.63212    6.31300   95.74660    0.09375 101386.97660
 20180101-01:00    5.31340   -0.73485    5.36400   97.87410    0.01562 101388.50000
 20180101-02:00    6.08910   -0.29475    6.09630   92.77130    0.01562 101405.36720
 20180101-03:00    5.87680    0.02163    5.87690   89.78910    0.00000 101369.95310
 20180101-04:00    5.69520    0.05114    5.69540   89.48550    0.00000 101313.44530

The data I want to summarize is
UV and DIR. [UV] is the wind speed and [DIR] is the direction of the wind. Based on the true north, it represents the direction of the wind clockwise, such as 90 degrees east and 180 degrees south.

To summarize, wind speed [UV] is 6 m/s or higher and
The wind direction [DIR] is east to south, that is, 90° to 180°
Counting the number that meets the .

I used to do something similar in the past with a csv file, but it changed to a txt file and stumbles.
The programs that run the old csv files are as follows:

import csv

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

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

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

print(count)
    f.close()

I am currently writing the following program, but it doesn't work well.

f=open("Fuya Ohashi 2018.txt", "r")

Direction = [ range (90,180) ]

count = 0
for row in f.readlines():
  if(col[42]>=6) and(col[52] in Direction):
    count+=1

print(count)
    f.close()

The error name colis not defined appears when you run it.
I stopped because I didn't know how to express the line I wanted to read.

This may be a rudimentary question, but please answer it.

f=open("Fuya Ohashi 2018.txt", "r")

dir_min = 90
dir_max = 180

count = 0
for line in f.readlines():
    # skip heads
    if any(char.isalpha() for char inline):
        continue

    date,uu,vv,uv,direction,tpr,msl=line.split()

    if float(uv)>=6 and dir_min<float(direction)<dir_max:
        count+=1

print(count)
f.close()

Thank you for your reply, it went well.
In addition, what does the following sentence mean?

if any(char.isalpha() for char inline):

python

2022-09-30 21:46

1 Answers

That's why I think it works like this.

f=open("Fuya Ohashi 2018.txt", "r")

dir_min = 90
dir_max = 180

count = 0
for line in f.readlines():
    # skip heads
    if any(char.isalpha() for char inline):
        continue

    date,uu,vv,uv,direction,tpr,msl=line.split()

    if float(uv)>=6 and dir_min<float(direction)<dir_max:
        count+=1

print(count)
f.close()


2022-09-30 21:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.