def save_to_file(pic_str,filename): outfile = open(filename, 'a') outfile.write(pic_str) outfile.close
时间: 2023-11-21 11:06:21 浏览: 66
这段代码实现了一个函数,用于将字符串写入指定的文件中。具体来说,该函数首先通过open()方法创建一个文件对象outfile,用于将数据写入指定的文件中。其中,'a'参数表示以追加模式打开文件,即将数据追加到文件末尾,而不是覆盖原有数据。然后,通过outfile.write()方法将字符串pic_str写入文件中。最后,使用outfile.close()方法关闭文件对象,释放资源。需要注意的是,如果指定的文件不存在,open()方法会自动创建一个新文件。如果需要每次写入数据时都清空原有数据,可以使用'w'参数打开文件。
相关问题
import argparse import numpy as np from openeye import oechem def clear_stereochemistry(mol): clear_atom_stereochemistry(mol) clear_bond_sterochemistry(mol) oechem.OESuppressHydrogens(mol, False, False, False) def clear_atom_stereochemistry(mol): for atom in mol.GetAtoms(): chiral = atom.IsChiral() stereo = oechem.OEAtomStereo_Undefined v = [] for nbr in atom.GetAtoms(): v.append(nbr) if atom.HasStereoSpecified(oechem.OEAtomStereo_Tetrahedral): stereo = atom.GetStereo(v, oechem.OEAtomStereo_Tetrahedral) if chiral or stereo != oechem.OEAtomStereo_Undefined: atom.SetStereo(v, oechem.OEAtomStereo_Tetrahedral, oechem.OEAtomStereo_Undefined) def clear_bond_sterochemistry(mol): for bond in mol.GetBonds(): if bond.HasStereoSpecified(oechem.OEBondStereo_CisTrans): for atomB in bond.GetBgn().GetAtoms(): if atomB == bond.GetEnd(): continue for atomE in bond.GetEnd().GetAtoms(): if atomE == bond.GetBgn(): continue v = [] v.append(atomB) v.append(atomE) stereo = bond.SetStereo(v, oechem.OEBondStereo_CisTrans, oechem.OEBondStereo_Undefined) def abs_smi(x): mol = oechem.OEGraphMol() if oechem.OESmilesToMol(mol, x): clear_stereochemistry(mol) return oechem.OEMolToSmiles(mol) else: return np.nan if __name__ == '__main__': parser = argparse.ArgumentParser(description="Remove stereochemistry from the input data set.") parser.add_argument("--in",dest="infile",help="whitespace-delimited input file",metavar="in.csv") parser.add_argument("--out", dest="outfile", help="output file", metavar="out.csv") args = parser.parse_args() n=0 with open(args.infile, 'r') as ifs: with open(args.outfile, 'w') as ofs: for line in ifs: if n==0: ofs.write(line) n=1 else: parsed = line.strip().split(',') if ('.' not in parsed[0]): ofs.write(f"{abs_smi(parsed[0])},{parsed[1]}\n")
这段代码是一个用于清除分子中立体化学信息的 Python 脚本。它使用了 OpenEye 包中的 oechem 模块来操作分子,并可以通过命令行参数指定输入输出文件。
脚本的主要功能在于定义了三个函数:`clear_stereochemistry`、`clear_atom_stereochemistry` 和 `clear_bond_sterochemistry`,它们分别用于清除分子中的原子立体信息、键立体信息和所有立体信息。这些函数都是通过遍历分子的原子和键,检查是否存在立体信息并将其清除来实现的。
另外,脚本还定义了一个名为 `abs_smi` 的函数,该函数将 SMILES 格式的分子字符串作为输入,并返回一个没有立体信息的 SMILES 字符串。它首先将输入的 SMILES 字符串转换为分子对象,然后调用之前定义的 `clear_stereochemistry` 函数清除立体信息,最后将分子对象转换回 SMILES 格式并返回。
在 `if __name__ == '__main__':` 语句块中,脚本使用 argparse 模块解析命令行参数,然后打开输入文件读取数据并将结果写入输出文件。对于每一行数据,脚本使用 `abs_smi` 函数先清除分子中的立体信息,然后将处理后的 SMILES 字符串写入输出文件。
import osdef mergeFile(folder_path, output_file): # 获取文件夹下的所有文件 file_list = sorted(os.listdir(folder_path)) # 按照文件名排序 with open(output_file, 'w') as outfile: for file_name in file_list: file_path = os.path.join(folder_path, file_name) if os.path.isfile(file_path): with open(file_path,r') as infile: outfile.write(infile.read()) outfile.write('\n') # 可根据需要添加分隔符
你好!这段代码是用于合并指定文件夹中的所有文件内容并输出到一个文件中。函数名为`mergeFile`,接受两个参数:`folder_path`表示文件夹路径,`output_file`表示输出文件路径。
首先,代码通过`os.listdir`获取指定文件夹下的所有文件,并使用`sorted`对文件名进行排序。然后,使用`with open(output_file, 'w') as outfile:`打开输出文件,将文件句柄赋值给`outfile`。
接下来,使用一个循环遍历文件列表中的每个文件名。通过`os.path.join(folder_path, file_name)`获取文件的完整路径。然后,判断该路径是否为文件,如果是,则使用`with open(file_path, 'r') as infile:`打开文件,并将其内容写入输出文件中,使用`outfile.write(infile.read())`。最后,每个文件内容写入完后,添加一个换行符(`\n`)作为分隔符。
根据需要,你可以修改代码中的分隔符或添加其他操作。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)