Python Forum
I am not getting any output in the Subset_soil_param.txt file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I am not getting any output in the Subset_soil_param.txt file
#1
Hello,
I am new to Python and to this forum, so I need some help with the following code:
original_soil_parameter_file = open('D:\Spring 2020\VIC\Parameter_files\original_soil_param.txt', "r")
Grid_Cell_id = open('D:\Spring 2020\VIC\Parameter_files\Grid_Cells.txt', "r")
Subset_soil_param = open('D:\Spring 2020\VIC\Parameter_files\subset_soil_param.txt', "w")
with open('D:\Spring 2020\VIC\Parameter_files\original_soil_param.txt') as f:
    for line in f:
        a = line.split(' ')
        if a[1] == Grid_Cell_id:
            Subset_soil_param.write(line)
Subset_soil_param.close()
Basically, I have an original file (named as original_soil_parameter_file), which covers the whole North Western United States. And I want to subset the file based on my research area. The original file contains rows of values with each values separated by a space. In order to subset I provided another text file to the code and called it Grid_cell_id. Then I used the for loop to match the second value (a[1]) with the values in Grid_Cell_id, so that after a finding an identical grid cell id in both files, the code will start saving the lines in the new file named Subset_soil_param.txt. After I run code, the Subset_soil_param is created but it's empty. I get the following output:
Output:
runfile('D:/Spring 2020/VIC/Parameter_files/subset_soil_param.py', wdir='D:/Spring 2020/VIC/Parameter_files')
Reply
#2
well if it's empty your logic statement is wrong:
Quote:if a[1] == Grid_Cell_id:
you explained a[1] but not grid_cell_id, what value is that? you read the whole file.
How come you are not reading grid_cell_id line by line? Maybe you should read each file
create a new list for each and then write to a new file.
break it down is print(a[1]) and print(grid_cell_id) find out if you are getting the correct values. This is just theory...
Reply
#3
I modified the code like this:
Grid_Cell_id = list(open('Grid_Cells.txt', "r"))
Subset_soil_param = open('subset_soil_param.txt', "w")


with open('original_soil_param.txt') as f:
    for line in f:
        a = line.split(' ')
        if a[1] in Grid_Cell_id:
            Subset_soil_param.write(line)

Subset_soil_param.close()
But I am still getting an empty file.
Reply
#4
Grid_Cell_id is a file pointer. You don't ever read anything from the file. Reading a file is in every beginner tutorial.
Reply
#5
(Jan-14-2020, 06:37 AM)woooee Wrote: Grid_Cell_id is a file pointer. You don't ever read anything from the file. Reading a file is in every beginner tutorial.
So, should I just remove the "r" from that line?
Reply
#6
(Jan-14-2020, 06:37 AM)woooee Wrote: Grid_Cell_id is a file pointer. You don't ever read anything from the file. Reading a file is in every beginner tutorial.
actually it's a list in their last post (right before your post)

@Baloch: when you read the Grid_Cells.txt into a list each element is one row, incl. the line ending (i.e. '\n'). It's not clear what is the format of your files, but I would safely bet that a[1] does not much any row (incl. the '\n')
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(Jan-14-2020, 07:07 AM)buran Wrote:
(Jan-14-2020, 06:37 AM)woooee Wrote: Grid_Cell_id is a file pointer. You don't ever read anything from the file. Reading a file is in every beginner tutorial.
actually it's a list in their last post (right before your post)

@Baloch: when you read the Grid_Cells.txt into a list each element is one row, incl. the line ending (i.e. '\n'). It's not clear what is the format of your files, but I would safely bet that a[1] does not much any row (incl. the '\n')

Below is a sample of the original_soil_param.txt, the values are separated by a single space, to further clarify second value i.e 240493 (represents grid_cell_id). This is the first row of the data. So, the concept of the code is to match the second value of each with the grid_cell ids present in the Grid_cells.txt, and when the code finds a match, it would pick up that particular row and write it in the subset_soil_param.

1 240493 41.21875 -116.21875 0.1000 0.767791 0.400832 0.673064 2 13.6030 13.6030 13.6030 473.0640 473.0640 473.0640 -99 -99 -99 21.4270 64.2820 214.2750 1821.3800 0.1000 0.3000 0.7118 6.0880 4.0000 11.1500 11.1500 11.1500 0.4100 0.4100 0.4100 1485.7000 1485.7000 1485.7000 2620.2800 2620.2800 2620.2800 -8 0.3920 0.3920 0.3920 0.2560 0.2560 0.2560 0.0100 0.0300 458.8940 0 0 0 0 19.0384

Here's a sample of the Grid_cell.txt:

240493
288832
287904
287909
286976
Reply
#8
not tested but
with open('Grid_cell.txt') as f:
    grid_cells = [line.strip() for line in f]
 
with open('original_soil_param.txt') as f, open('subset_soil_param.txt', "w") as sub_f:
    for line in f:
        cell_id = line.split(' ')[1]
        if cell_id in grid_cells:
            sub_f.write(line)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
(Jan-14-2020, 08:14 AM)buran Wrote: not tested but
with open('Grid_cell.txt') as f:
    grid_cells = [line.strip() for line in f]
 
with open('original_soil_param.txt') as f, open('subset_soil_param.txt', "w") as sub_f:
    for line in f:
        cell_id = line.split(' ')[1]
        if cell_id in grid_cells:
            sub_f.write(line)

Buran, man it worked, thank you so much. Clap
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,105 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Output CSV file with filepath and file contents glittergirl 1 1,753 Aug-03-2020, 01:50 AM
Last Post: glittergirl
  csv file output rturus 7 3,246 Jan-30-2020, 02:09 PM
Last Post: buran
  File name as part of output file name Jeeping_Coder 1 2,117 Jan-10-2020, 03:43 PM
Last Post: Clunk_Head
  How to extract a matrix from .xml.gz file to a excel file or any other output? enyrb 0 2,058 Oct-21-2019, 01:01 PM
Last Post: enyrb
  python file output to log file Rsh 4 3,690 Aug-13-2019, 09:00 PM
Last Post: DeaD_EyE
  Output SQL to csv or xls file? JP_ROMANO 4 2,592 Aug-02-2019, 01:58 AM
Last Post: JP_ROMANO

Forum Jump:

User Panel Messages

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