Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9155367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:34:54+00:00 2026-06-17T12:34:54+00:00

I am using the script below to extract data from some trajectory file and

  • 0

I am using the script below to extract data from some trajectory file and dump the data into new files files. I can use directive “>” to redirect results into a file but I need to do like this for more then 2000 files then. For make things easy I tried to open a file and let the python itself direct the results into a file.

To achieve that I added a line, at the place, (##) in the code, to open a file as shown below.
Also I added a line to direct the results into a file as shown in the code below at line contains (###).

#!/usr/bin/env python
'''
always put -- #!/usr/bin/env python -- at the shebang
'''

from Scientific.IO.NetCDF import NetCDFFile as Dataset
import itertools as itx


inputfile = "../traj-waters/waters1445-MD001-run1000.traj"


for FRAMES in range(0,2):
   frame = FRAMES

   text_file = open("velo-Output.dat", "w")   (##)

   #inputfile = 'mdcrd'
   ppp = inputfile

   def grouper(n, iterable, fillvalue=None):
       args = [iter(iterable)] * n
       return itx.izip_longest(fillvalue=fillvalue, *args)

   formatxyz = "%12.7f%12.7f%12.7f%12.7f%12.7f%12.7f"
   formatxyz_size = 6
   formatxyzshort = "%12.7f%12.7f%12.7f"
   formatxyzshort_size = 3

   #ncfile = Dataset(inpitfile, 'r')
   ncfile = Dataset(ppp, 'r')

   variableNames = ncfile.variables.keys()
   #print variableNames

   shape = ncfile.variables['coordinates'].shape
   '''
   do the header
   '''

   print 'title ' + str(frame)
   print "%5i%15.7e" % (shape[1],ncfile.variables['time'][frame])


   '''
   do the velocities
   '''
   try:
       xyz = ncfile.variables['velocities'][frame]
       temp = grouper(2, xyz, "")

       for i in temp:
           z = tuple(itx.chain(*i))
           if (len(z) == formatxyz_size): 
               print formatxyz % z
               text_file.write('formatxyz\n' % z))  (###)
           elif (len(z) == formatxyzshort_size): print formatxyzshort % z


   except(KeyError):

       xyz = [0] * shape[2] 
       xyz = [xyz] * shape[1]
       temp = grouper(2, xyz, "")

       for i in temp:
           z = tuple(itx.chain(*i))
           if (len(z) == formatxyz_size): print formatxyz % z
           elif (len(z) == formatxyzshort_size): print formatxyzshort % z
           x = ncfile.variables['cell_angles'][frame]
           y = ncfile.variables['cell_lengths'][frame]
   text_file.close()

But I get error if I run this code as below.

  Traceback (most recent call last):
File "./Nctorst-onlyVelo.py", line 73, in <module>
  text_file.write(str('formatxyz\n' % z))
TypeError: not all arguments converted during string formatting

Since I am newbie into python I find lost to correct this issue.

Advance thanks for help.
Regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:34:55+00:00Added an answer on June 17, 2026 at 12:34 pm

    Either you made a Typo, or did not understand how string formating with replacement operator works

    The Line

    text_file.write('formatxyz\n' % z))  (###)
    

    Should be written similar to the previous line

    text_file.write(formatxyz % z + '\n')  (###)
    

    Also you should look forward to use the Format String Syntax

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried using a example file using PHPExcel script to extract data from
Using the script task below to stuff list of files into User::ftp_file_list. But, the
I'm using the script below, so user can upload their profile picture. The first
I am using the script code below to get results from my database based
using the script code below, this returns the images from facebook vertically. ive removed
I am using below script for spliting a large zip file in small chucks.
I'm fairly new to jQuery and I'm using the script below. Basically it uses
I am trying to create a BASH script what would extract the data from
I have a 1.3GB text file that I need to extract some information from
I'm using this script below for loading a hidden div, inside another: $(document).ready(function(){ $(function()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.