Search code examples
pythondatabaseloopsdelimiter

Loop through .txt to create two delimiters depending on data index


So I have a number of files (1000ish) each with 90k plus lines, where the data was recorded in the incorrect format and I am trying to make reformat the txt files.

The data currently looks like this:

9/3/2021 00 00 00 -0.18 -2.40 -2.40
9/3/2021 00 00 01 -0.18 -2.40 -2.40
9/3/2021 00 00 02 -0.18 -2.40 -2.40
9/3/2021 00 00 03 -0.17 -2.40 -2.40
.....

However, it should look like

9/3/2021,00:00:00,-0.18, , ,-2.40,-2.40
9/3/2021,00:00:01,-0.18, , ,-2.40,-2.40
9/3/2021,00:00:02,-0.18, , ,-2.40,-2.40
9/3/2021,00:00:03,-0.17, , ,-2.40,-2.40
....

I have managed to go through everything and add a " , " with

input = open(os.path.expanduser("~/Desktop/ssdat/SegY TidalCorrection 03092021.txt"))
content = input.read()
content = content.replace(" ", ",")
print(content)

resulting in an output of:
9/3/2021,17,22,47,0.20,1511.10,-2.12,-2.29
9/3/2021,17,22,48,0.01,1511.10,-2.29,-2.29
9/3/2021,17,22,49,-0.17,1511.05,-2.41,-2.29
9/3/2021,17,22,50,-0.14,1511.02,-2.34,-2.30

So I guess my question is how can I loop through everything to make two new delimiters?


Solution

  • Similar to @bret-hogg's answer. But if you're only interested in recreating a simple string on each line then the following will work:

    import os
    
    
    with open("your_old_file.txt", "r") as text_file, open("your_new_file.txt", "w") as csv_file:
        for line in text_file:
            fields = line.split(" ")
            date = fields[0]
            time = fields[1] + ":" + fields[2] + ":" + fields[3]
            replacement_row = f"{date},{time},{fields[4]},,,{fields[5]},{fields[6]}"
            csv_file.write(replacement_row)