Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Data window
#1
Hi guys,


I have a huge data file that the first two columns are coordinates (latitude, longitude). I look for a way to extract the data window from the original file giving maximum and minimum values to the coordinates.

Here's an input example:

Output:
10.0 20.0 1 1 15.0 25.0 1 1 20.0 30.0 1 1 25.0 35.0 1 1 30.0 40.0 1 1 35.0 45.0 1 1 40.0 50.0 1 1 45.0 55.0 1 1 50.0 60.0 1 1 55.0 65.0 1 1
I want to write a file only with data which the latitude values (first column) are between 20.0 and 40.0, and the longitude values (second column) are between 30.0 and 50.0.

Here's the desire output:

Output:
20.0 30.0 1 1 25.0 35.0 1 1 30.0 40.0 1 1 35.0 45.0 1 1 40.0 50.0 1 1
Here's what I tried, but nothing is print on the exit file:

#!/usr/bin/env python3

with open("entrada.txt","r") as fh, open("windowexit.txt","a") as sh:
    for row in fh:
        if float(row[0])>=20.0 and float(row[0])<=40.0 and float(row[1])>=30.0 and float(row[1])<=50.0:
            print(row, file = sh, end='')
Thanks for the help. XD
Reply
#2
In this case (i.e. without using csv module) the row is just a string. So row[0] is the first char (e.g. '1' in the first input row) and row[1] is the second char, etc. That is why your if statement never evaluates to TRUE and nothing is printed to the output file.

you need something like
for row in fh:
   row_list = row.split(' ')
   if float(row_list[0])>=20.0 and float(row_list[0])<=40.0 and float(row_list[1])>=30.0 and float(row_list[1])<=50.0:
       print(row, file = sh, end='')
Reply
#3
(Jan-17-2017, 11:24 AM)buran Wrote: In this case (i.e. without using csv module) the row is just a string. So row[0] is the first char (e.g. '1' in the first input row) and row[1] is the second char, etc. That is why your if statement never evaluates to TRUE and nothing is printed to the output file.

Thanks !! That's works to me.

I will try with the real file to test if I will have problems with char position.
Reply
#4
In this case you cannot implement the check for min an max value anyway. Your original code will not work even if row is a list. If your input file has a header, but fields may have different positions in different files, then you may want to use csv.DictReader.

Also, if they are in column 12 and 13, why do you check column 0 and ? Your example input has only 4 columns, not 14 (i.e. assuming column with index 13 is the last one)
Reply
#5
(Jan-17-2017, 11:36 AM)buran Wrote: In this case you cannot implement the check for min an max value anyway. Your original code will not work even if row is a list. If your input file has a header, but fields may have different positions in different files, then you may want to use csv.DictReader.

Also, if they are in column 12 and 13, why do you check column 0 and ? Your example input has only 4 columns, not 14 (i.e. assuming column with index 13 is the last one)

Sorry for my late edition on my previous reply. Yes, my file has a header and the fields have the same positions in different files. I'm only concerning about char positions.

I'm trying your previous suggestion. I'll look for instructions for use csv.DictReader too. Thanks !!
Reply
#6
Excuse me, wasn't best answer, mods can delete post.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a way to call and focus any popup window outside of the main window app? Valjean 6 1,742 Oct-02-2023, 04:11 PM
Last Post: deanhystad
  Pyspark Window: perform sum over a window with specific conditions Shena76 0 1,166 Jun-13-2022, 08:59 AM
Last Post: Shena76

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020