I have been trying for some time now to partially automate a process in an effort to save time. Specifically, I am attempting to take simplified weather soundings from the Integrated Global Radiosonde Archive and convert them to a format that is compatible with the Weather Research and Forecasting model through a JupyterHub terminal with Python. I have already successfully implemented a version of the code which allows for the transformation of each line of data from the IGRA form to the WRF-compatible form. However, the current version of this code only allows for line-by-line conversion, which I am hoping to turn into full text file conversions. While the IGRA output format is itself static, the way the data populates a text file ultimately results in slight variations when the data is further stripped from the source. While the delimiter is a static " ", the length of individual strings within each line may slightly vary, which prevents the utilization of length functions to automate the process. As an example, here are three different lines from a choice stripped IGRA sounding: 85000 1554 224 90 225 57 70000 3206 94 70 205 82 50000 5910 -67 350 245 57 Each of these values are integral to the performing of various calculations necessary for converting the sounding from the IGRA format into the WRF-compatible format, since the IGRA output variables are different than the WRF input variables. Given that these variables are saved into a base file "IGRASounding.txt", I would need to read the text file in such a way that each individual line is read and isolated from the other lines. After this is performed, the line would need split along the delimiter in such a way that the now-split text strings could be attached to variables, which I could then manipulate to get the necessary variables. Once acquired, I would then read the new variables in sequence into a new text file "WRFSounding.txt" before proceeding to the next line until the file is completed. The penultimate goal is to place all of this within a larger function which I could then use for file management and a flag system to alter various components of the calculations, expanding upon conversion code that I have already written. Solutions I have found thus far primarily focus on reading through small files which can be easily split into variables based on delimiter, the best solution to which I have been able to find here: Split line into variables However, while this did answer my question for how to place variables from the text string into variables, I am still at a loss for how to have the code be read line-by-line while also performing this function. I suspect the coding result would take a similar form to this following mock-up: def modify_and_write_file(input_filename, output_filename): file = open(input_filename, 'r') for line in file.readlines(): P = [] gph = [] InstTemp = [] DPTDEP = [] wdir = [] wspd = [] P, gph, InstTemp, DPTDEP, wdir, wspd = line.strip().split(" ") !ConversionCode Functions if line(i = 1): outfile = open(output_filename, 'w') outfile.writelines(gph, u, v, T, q, P) #WRF requires surface pressure; does not need included in code and can be manually done if necessary elif: outfile = open(output_filename, 'w'): outfile.writelines(gph, u, v, T, q) Current attempts using my current code structure results in an error describing that there are too many values to unpack, as it currently only expects 6 - one for each declared variable; traceback below. I suspect this is because the lines are still being treated as a single entity as opposed to the split entities I am attempting to have them be so I can further process them. ----> 1 modify_and_write_file('/home/jovyan/Soundings/Sounding00z.txt','/home/jovyan/Soundings/Sounding00z_copy.txt') Cell In[45], line 17, in modify_and_write_file(input_filename, output_filename) 15 wdir = [] 16 wspd = [] ---> 17 P, gph, InstTemp, DPTDEP, wdir, wspd = line.strip().split(" ") 18 print(P) 19 modified_lines = [] ValueError: too many values to unpack (expected 6) As a simple example, let wspd and wdir be 5 and 30, respectively. u and v in the output then become 3 and 4 after being processed through the following code: import numpy as np u = wspd*np.cos(np.deg2rad(wdir)) v = wspd*np.sin(np.deg2rad(wdir)) If, however, wspd and wdir change in sequence in the following manner in the data: 5 30 5 60 Then an attempt to utilize adapted code for the example above would find too many values to unpack, since it would be attempting to load in four values as opposed to having it load in two target values for processing in sequence. For testing, the expected (u,v) sequence should be: 3 4 4 3 My guess is that I'm either missing something relatively basic with looping functions (by line, and then along each line), that my knowledge of file I/O is inadequate for what I'm trying to do, or both. Any help would be appreciated. Continue reading...