简体   繁体   English

使用 excel 表格数据制作 txt 文件

[英]Making a txt file using excel sheet data

excel image file excel镜像文件

txt image file txt 图像文件

I am a biology student and have no experience in coding.我是一名生物学专业的学生,​​没有编码经验。 I want to make txt files from specific data from excel files.我想从excel文件中的特定数据制作txt文件。 The data in the txt file should be in the format given in the txt file where 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)

and

size_x = 60
size_y = 60
size_z = 60

energy_range = 10

How can I make a python script which can do this for 1000 excel files?如何制作一个 python 脚本,它可以为 1000 个 excel 文件执行此操作? Any help will be highly appreciated.任何帮助将不胜感激。

First do it for single file and put code in function which get filename as argumetn - ie.首先对单个文件执行此操作,并将代码放入 function 中,文件名为 argumetn - 即。 function(filename) - and later use for -loop to execute it for many filenames on list. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM