![](/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.