By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,922 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,922 IT Pros & Developers. It's quick & easy.

Parsing attributes from adf.txt files

P: 16
I would like to parse only the first line of each adf.txt files (i.e. attributes) . This python code could print individually for each file type, but i would like to run in one python code for all adf.txt files where the first line starts with only 4 different attribute terms like 'Composite Element Name | Block Column | Reporter Name | CompositeSequence Identifier' for all 35 different adf.txt files. I hereby attached only a zip file for 4 adf.txt files but for more files you may locate at this link Could you please rectify this script . I would be glad for your support and cooperation.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2. import glob
  3. #import linecache
  4. outfile = open('out_att.txt' , 'w')
  5. files = glob.glob('*.adf.txt')
  6. for file in files:
  7.     infile = open(file)
  8.     #count = 0
  9.     for line in infile:
  11.         lineArray = line.rstrip()
  12.         if not line.startswith('Composite Element Name') : continue
  13.         #count = count + 1
  14.         lineArray = line.split('%s\t')
  15.         print lineArray[0]
  16.         output = "%s\t\n"%(lineArray[0])
  17.         outfile.write(output)
  18.     infile.close()
  19. outfile.close()          
Attached Files
File Type: zip arrayexpress(2).zip (504.3 KB, 41 views)
File Type: txt out_att.txt (178 Bytes, 361 views)
Oct 19 '10 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 624
Note that this line does nothing, i.e. is backwards (use if-->do something), and TONYJV usually produces good code:
Expand|Select|Wrap|Line Numbers
  1.         if not line.startswith('Composite Element Name') : continue 
Perhaps you meant something like
Expand|Select|Wrap|Line Numbers
  1.     fp = open(file)
  2.     line = fp.readline()     ## reads first rec only 
From here on you will have to produce your own code. Taking code from one forum and presenting it on another forum as your own is not ethical.
Oct 19 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.