I am trying to develop a function for my application which would use already opened file (fin) and read certain line from it. Each line in the text starting from given keywords (pos1). Finally this function should return a print of all line containing the keywords pos1. But apparently "linecache" return only space.
Do somebody know a way to deal with line in the text file differently or know a solution to my problem with linecache? Thank Beforehand!!!

def CifT(self):
        import linecache
        global fin   #opened file
        start = 1.0
        pos1 = ["_chemical_formula_moiety", "_chemical_formula_weight", "_symmetry_cell_setting", 
        "_symmetry_space_group_name_H-M ", "_cell_length_a","_cell_length_b", "_cell_length_c", "_cell_angle_alpha",
        "_cell_angle_beta", "_cell_angle_gamma", "_cell_volume", "_cell_formula_units_Z", "_cell_measurement_temperature",
        "_cell_measurement_theta_min", "_cell_measurement_theta_max", "_exptl_absorpt_coefficient_mu", "_diffrn_radiation_type", 
        "_refine_ls_R_factor_all", "_refine_ls_R_factor_gt", "_refine_ls_wR_factor_ref", "_refine_ls_wR_factor_gt", "_refine_ls_goodness_of_fit_ref"]
        for i in pos1:
          while 1:
            pos = self.text.search(i, start, stopindex=END)
            if not pos:
              break
            print pos[:2] #print a number of line for each keyword
            line=linecache.getline("fin", pos:2)  #read line 
            print line
            start = pos + "+1c"

Recommended Answers

All 4 Replies

If I understand correctly, you should read a record, look for one of the strings, and print the line. Something like the following should be close to what you want.

## pass "fin" to the function and avoid the use of globals
def CifT(self, fin):
    pos1 = ["_chemical_formula_moiety", "_chemical_formula_weight", "_symmetry_cell_setting", 
        "_symmetry_space_group_name_H-M ", "_cell_length_a","_cell_length_b", "_cell_length_c", "_cell_angle_alpha",
        "_cell_angle_beta", "_cell_angle_gamma", "_cell_volume", "_cell_formula_units_Z", "_cell_measurement_temperature",
        "_cell_measurement_theta_min", "_cell_measurement_theta_max", "_exptl_absorpt_coefficient_mu", "_diffrn_radiation_type", 
        "_refine_ls_R_factor_all", "_refine_ls_R_factor_gt", "_refine_ls_wR_factor_ref", "_refine_ls_wR_factor_gt", "_refine_ls_goodness_of_fit_ref"]

    for rec_num, rec in enumerate(fin):
        rec = rec.strip()
        start = 0
        for phrase in pos1:
          while 1:
            pos = rec.find(phrase, start)
            if  pos < 0:
              break

            print rec_num, phrase, pos  #print a number of line for each keyword
            print rec
            start = pos + len(phrase)

WOW !!!! it looks much simpler and Working THANKS ALLOT !!!!!

I was so happy to see the result that even did not draw attention what it returns.
This function returns next text 25 (instead of ones) times

chemical_formula_moiety
/
_chemical_formula_weight
/
_symmetry_cell_setting
/
_symmetry_space_group_name_H-M 
/
_cell_length_a
/
_cell_length_b
/
_cell_length_c
/
_cell_angle_alpha
/
_cell_angle_beta
/
_cell_angle_gamma
/
_cell_volume
/
_cell_formula_units_Z
/
_cell_measurement_temperature
/
_cell_measurement_theta_min
/
_cell_measurement_theta_max
/
_exptl_absorpt_coefficient_mu
/
_diffrn_radiation_type
/
_refine_ls_R_factor_all
/
_refine_ls_R_factor_gt
/
_refine_ls_wR_factor_ref
/
_refine_ls_wR_factor_gt
/
_refine_ls_goodness_of_fit_ref
/

At the same time function returns only a keyword but do not return a whole line in the text. Maybe if I show to you a file I am trying to read it would be more clear :

cell_length_a 12.3799(16)
_cell_length_b 12.9436(16)
_cell_length_c 33.939(4)
_cell_angle_alpha 90.00
_cell_angle_beta 95.896(2)
_cell_angle_gamma 90.00
_cell_volume 5409.6(12)
_cell_formula_units_Z 8
_cell_measurement_temperature 150(2)
_cell_measurement_reflns_used ?
_cell_measurement_theta_min ?
_cell_measurement_theta_max ?

_exptl_crystal_description ?
_exptl_crystal_colour ?
_exptl_crystal_size_max 0.15
_exptl_crystal_size_mid 0.12
_exptl_crystal_size_min 0.12
_exptl_crystal_density_meas ?
_exptl_crystal_density_diffrn 1.326
_exptl_crystal_density_method 'not measured'
_exptl_crystal_F_000 2256
_exptl_absorpt_coefficient_mu 0.866
_exptl_absorpt_correction_type ?
_exptl_absorpt_correction_T_min 0.8811
_exptl_absorpt_correction_T_max 0.9032
_exptl_absorpt_process_details ?

_exptl_special_details
;
?
;

_diffrn_ambient_temperature 150(2)
_diffrn_radiation_wavelength 0.71073
_diffrn_radiation_type MoK\a
_diffrn_radiation_source 'fine-focus sealed tube'
_diffrn_radiation_monochromator graphite
_diffrn_measurement_device_type ?
_diffrn_measurement_method ?
_diffrn_detector_area_resol_mean ?
_diffrn_reflns_number 70013
_diffrn_reflns_av_R_equivalents 0.0694
_diffrn_reflns_av_sigmaI/netI 0.0681
_diffrn_reflns_limit_h_min -16
_diffrn_reflns_limit_h_max 16
_diffrn_reflns_limit_k_min -16
_diffrn_reflns_limit_k_max 16
_diffrn_reflns_limit_l_min -44
_diffrn_reflns_limit_l_max 44
_diffrn_reflns_theta_min 1.21
_diffrn_reflns_theta_max 27.53
_reflns_number_total 12426
_reflns_number_gt 8204
_reflns_threshold_expression >2sigma(I)


expected result: program should find a line with given keyword and display it.

Thanks for Help Anyway!

I think I figure it out:

f = open("/home/denis/ds67/dens.cif", 'r')
pos1 = ["_chemical_formula_moiety", "_chemical_formula_weight", "_symmetry_cell_setting", 
          "_symmetry_space_group_name_H-M ", "_cell_length_a","_cell_length_b", "_cell_length_c", "_cell_angle_alpha",
          "_cell_angle_beta", "_cell_angle_gamma", "_cell_volume", "_cell_formula_units_Z", "_cell_measurement_temperature",
          "_cell_measurement_theta_min", "_cell_measurement_theta_max", "_exptl_absorpt_coefficient_mu", "_diffrn_radiation_type", 
          "_refine_ls_R_factor_all", "_refine_ls_R_factor_gt", "_refine_ls_wR_factor_ref", "_refine_ls_wR_factor_gt", "_refine_ls_goodness_of_fit_ref"]
lines = f.readlines()
for line in lines:
    for i in pos1:
      # print i
       if(line.find(i)!=-1):
         print line
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.