Home > front end >  Formatting Large Text File with Python
Formatting Large Text File with Python

Time:02-14

So upfront I am very much a novice with Python. Just wanted to get that out there.

To describe my question, I am looking for a way to read a data from a text file with various headers and reformat the data. The each line of data consists of XYZ coordinates with an associated value for that particular coordinate as shown below:

    Header 1...
    Header 2...
         X1 Y1 Z1 Value1
         X2 Y2 Z2 Value2
         etc...

what I am wanting to do is reformat the data in a manner that can be read by another program such as:

    Header 1...
    Header 2...
         X1 Y1 Z1
         X2 Y2 Z2
         etc...
    essentially a comment/header...
         Value1
         Value2
         etc...**

I am just looking for some direction on how to start tackling this problem. The text files I am looking to edit range in size from several thousand lines to very large files with 100,000,000 lines of data. So it would not matter to me if the script takes a while to run because you can imagine the time it would take to edit a file that size by hand!

Thank you for your time and help in advance, Luke H

CodePudding user response:

Here's my idea:

You can use a variable to store the current header. And for each line within that header, use string.split() function to seperate (X, Y, Z) and the value after the coordinates. Use a list of tuples to store that XYZ and the values after the XYZ can be stored in a seperated list.

Then you can loop through the data you've collected, write the headers first, and then join the X, Y, Z together using string.join() and add a tab to the beginning.

  • Related