![](/img/trans.png)
[英]How to import data from .txt file to a specifc excel sheet with Python?
[英]Making a txt file using excel sheet data
我是一名生物学专业的学生,没有编码经验。 我想从excel文件中的特定数据制作txt文件。 txt 文件中的数据应采用 txt 文件中给出的格式,其中
center_x = data from center_x and pocket 1 (col 7, row 2)
center_y = data from center_y and pocket 1 (col 8, row 2)
center_z = data from center_z and pocket 1 (col 9, row 2)
和
size_x = 60
size_y = 60
size_z = 60
energy_range = 10
如何制作一个 python 脚本,它可以为 1000 个 excel 文件执行此操作? 任何帮助将不胜感激。
首先对单个文件执行此操作,并将代码放入 function 中,文件名为 argumetn - 即。 function(filename)
- 稍后使用for
-loop 为列表中的许多文件名执行它。
import pandas as pd
def function(filename):
df = pd.read_csv(filename, sep='\s*,\s*', engine='python')
#print(df.columns)
row = df[ df['name'] == 'pocket1' ]
x = row['center_x'][0]
y = row['center_y'][0]
z = row['center_z'][0]
#print(x, y, z)
text = ""
text += f"center_x = {x}\n"
text += f"center_y = {y}\n"
text += f"center_z = {z}\n"
text += f"\n"
text += f"size_x = 60\n"
text += f"size_y = 60\n"
text += f"size_z = 60\n"
text += "\n"
text += "energy_range = 10\n"
print(text)
with open(filename + '.txt', 'w') as fh:
fh.write(text)
# --- main ---
#import os
#all_filenames = os.listdir()
#import glob
#all_filenames = glob.glob('*.csv')
all_filenames = ['structure.pdb_predictions.csv']
for filename in all_filenames:
function(filename)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.