Home > database >  Making a txt file using excel sheet data
Making a txt file using excel sheet data

Time:07-29

excel image file

txt image file

I am a biology student and have no experience in coding. I want to make txt files from specific data from excel files. The data in the txt file should be in the format given in the txt file where

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? Any help will be highly appreciated.

CodePudding user response:

First do it for single file and put code in function which get filename as argumetn - ie. function(filename) - and later use for-loop to execute it for many filenames on list.

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)    
  • Related