Python Forum

Full Version: I am not getting any output in the Subset_soil_param.txt file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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')
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...
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.
Grid_Cell_id is a file pointer. You don't ever read anything from the file. Reading a file is in every beginner tutorial.
(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?
(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')
(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
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)
(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