Text of discuss.python.org › t › adding-a-line-break › 61927 as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout.

Open the live page

Adding a Line Break? - Python Help - Discussions on Python.org

Adding a Line Break? Python Help help derekm22098 (derek) August 25, 2024, 4:37pm 1 Hello. I’m wondering if anyone could help me. I’ve tried \n and os.linsep and I cannot get a line break working. I’m brand new to Python and havent coded since Visual Basic 6 back in the 90s. I’m trying to output “IconStatement” with line breaks? import os import xarray as xr from glob import glob num_of_files = 3 glmFiles = glob(f'OR_GLM-L2-LCFA_G16_*.nc') print(glmFiles) flash_data = [] updateTime = None for i in range(num_of_files): if i < len(glmFiles): ds = xr.open_dataset(glmFiles[-i]) if updateTime == None or updateTime <= ds.product_time.data: updateTime = ds.product_time.data for lat, lon in zip(ds.flash_lat.data, ds.flash_lon.data): lon = float(lon) lat = float(lat) IconStatement = "Icon: " + str(lat) + ", " + str(lon) + ", 1, 1, 16, 16" + os.linesep flash_data.append((IconStatement)) with open(f'glm_strikes.txt', 'w') as f: data = (flash_data) f.write(str(data)) with open(f'glm_update.txt', 'w') as f: f.write(str(updateTime)) barry-scott (Barry Scott) August 25, 2024, 5:27pm 2 You have a list of strings that you want to print on there own line right?

But you did not loop over the lines to handle them. data = (flash_data) Note: the () does not change anything, its the same as: data = flash_data You likely want something like this: with open('glm_strikes.txt', 'w') as f: for line in flash_data: f.write(line) You use the f'...' string prefix, but not where you need to subsitute values. The place when an f-string would be useful is for this: IconStatement = f"Icon: {lat}, {lon}, 1, 1, 16, 16\n" Note: Use of os.linesep is not needed.

Python’s text mode files will translate line ending from \n to whatever the OS expects. derekm22098 (derek) August 25, 2024, 6:35pm 3 Thank you very much for your help! I understand my issue now. Have a good day! derekm22098 (derek) August 25, 2024, 6:48pm 6 Perfect! That code worked and I now have a better understanding of what I was doing wrong and learned something new. Thank you very much and have a great day. bschubert (Brian Schubert) August 25, 2024, 6:59pm 7 Just to add, you can also use writelines in place of a manual loop over the lines with write calls: with open('glm_strikes.txt', 'w') as f: f.writelines(flash_data) If the lines in flash_data don’t already have line separators added, you can add them while writing to the file by using print instead of write : with open('glm_strikes.txt', 'w') as f: for line in flash_data: print(line, file=f) If later on you find yourself using pathlib , you can avoid managing the open context-manager yourself by using Path.write_text : glm_file = Path("glm_strikes.txt") glm_file.write_text("\n".join(flash_data)) # add separators glm_file.write_text("".join(flash_data)) # don't add separators barry-scott (Barry Scott) August 26, 2024, 5:25am 8 Downside is the last line does not have a line separator. derekm22098 (derek) August 27, 2024, 3:02am 9 Oh, very nice!

Thank you for your help. I rewrote some of my coding with your suggestions and added other things I needed and it’s working great. My code is pulling lightning data from GOES satellite and making it available for free, for the public’s use, using expensive GR2Analyst & WSV3 software. High end weather software. So I really thank you and so do about 170 unique ips (users) of my data. Thank you!