Ad
I Need To Split A Seismological File So That I Have Multiple Subfiles
Basically what I have is an ASCII file containing seismological information of various earthquake events. Each event (with its specific info) is separated from the following by a jumpline.
What I want is to split this huge file using python into a series of subfiles that contain approximately 700 events each with its own information, and these subfiles must be chronologically organized.
The original file look like this:
You can see that between the first and second event is a jumpline and so every following events.
Thanks in advance for your help
Ad
Answer
In principle you can do like that:
inputFile = "AllEvents.txt" # give the path to the file that contain all the events
eventInfo = "" # create a string to hold event info
eventCounter = 0
fileId = 0
subFile= open("Events" + str(fileId) + ".txt","w+") # create the sub file that you need
with open(inputFile) as fileContent:
for line in fileContent:
if not line.strip(): # strip white spaces to be sure is an empty line
subFile.write(eventInfo + "\n") # add event to the subFile
eventInfo = "" # reinit event info
eventCounter += 1
if eventCounter == 700:
subFile.close()
fileId += 1
subFile = open("Events" + str(fileId) + ".txt","w+")
eventCounter = 0
else:
eventInfo += line
subFile.close()
At the end you will have a list of files, each with 700 events: Events0.txt, Events1.txt, ...
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad