Sponsored Content
Top Forums Programming Appending a column in xlsx file using Python Post 303000142 by durden_tyler on Wednesday 5th of July 2017 06:55:25 AM
Old 07-05-2017
Quote:
Originally Posted by nans
Great. I've changed it to 'while cell_pos.value:' and this works just perfect for my script.
Out of curiosity if I wanted to say go until all the cells E, G, and F are empty, how would I go about that ?
...
Congratulations!
Code:
while cell_pos.value:

will run the "while" loop as long as cell_pos.value is True that is, it is non-empty.

In order to check "all of cells E, G, F are empty" we use the logical operator "and" to combine the three cell values:
1) cell_pos.value
2) cell_alt.value
3) cell_ref.value

Such a condition is called a "compound" condition.
So:

Code:
while cell_pos.value and cell_alt.value and cell_ref.value:

will enter the "while" loop as long as all of cells E, G, F are non-empty i.e. they have some value in them. The moment any one of the cells E, G, F is empty, the loop stops.

and
Code:
while cell_pos.value or cell_alt.value or cell_ref.value:

will enter the "while" loop as long as any one of cells E, G, F is non-empty. The moment all of cells E, G, F are empty, the loop stops.

Your program will loop through the rows checking only pos value.
So if, in a row, the pos value is non-empty but alt and/or ref values are empty, it will still form the key and try to check if the key exists in the dictionary dict_pos.
This may or may not work, depending on how the dictionary was formed from "scores.txt" text file.

Here's the complete program for your reference:

Code:
#!/usr/bin/python
import os
import csv
from openpyxl import load_workbook
from datetime import datetime
from collections import namedtuple

# Variables
sheet_directory = '<absolute_path_till_sheet_directory>'
txt_file = '<absolute_path_till_text_directory>/scores.txt'

def process_xl_sheets():
    # Process the text file and form the dictionary of positions
    dict_pos = {}
    Scores = namedtuple("Scores", ["POS", "ALT", "REF"])
    first_line = True
    with open(txt_file) as txt_filename:
        for line in txt_filename:
            if not line.strip():   # Skip empty lines
                continue
            if first_line:         # Skip the header
                first_line = False
                continue
            line = line.rstrip('\n')
            x = line.split('\t')
            cpos = Scores(POS=x[0], ALT=x[2], REF=x[1])
            dict_pos[cpos] = x[3]

    # Now process all Excel files
    pos_col_no = 'E'
    alt_col_no = 'G'
    ref_col_no = 'F'
    score_col_no = 'V'
    row_no = 4
    for sheet_root, sheet_dirs, sheet_files in os.walk(sheet_directory):
        for sheet_file in sheet_files:
            if sheet_file.endswith('.xlsx'):
                sheet_xl_file = os.path.join(sheet_root, sheet_file)
                wb = load_workbook(sheet_xl_file, data_only=True)
                ws = wb.get_sheet_by_name('raw_data')
                pos = ws[pos_col_no + str(row_no)].value
                alt = ws[alt_col_no + str(row_no)].value
                ref = ws[ref_col_no + str(row_no)].value
                while pos or alt or ref:
                    cpos = Scores(POS=str(pos), ALT=alt, REF=ref)
                    if cpos in dict_pos:
                        ws[score_col_no + str(row_no)].value = dict_pos[cpos]
                    else:
                        ws[score_col_no + str(row_no)].value = 'Unknown_' + datetime.now().strftime("%B") + datetime.now().strftime("%Y")
                    row_no += 1
                    pos = ws[pos_col_no + str(row_no)].value
                    alt = ws[alt_col_no + str(row_no)].value
                    ref = ws[ref_col_no + str(row_no)].value
    wb.save(sheet_xl_file)

# Main section
process_xl_sheets()


Last edited by durden_tyler; 07-05-2017 at 01:10 PM.. Reason: You learn about the gaps in your thinking by reviewing what you wrote earlier... :)
This User Gave Thanks to durden_tyler For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Appending a column in one file to the corresponding line in a second

It appears that this has been asked and answered in similar fashions previously, but I am still unsure how to approach this. I have two files containing user information: fileA ttim:/home/ttim:Tiny Tim:632 ppinto:/home/ppinto:Pam Pinto:633 fileB ttim:xkfgjkd*&#^jhdfh... (3 Replies)
Discussion started by: suzannef
3 Replies

2. Shell Programming and Scripting

Appending 'string' to file as first column.

Hi , I have the below file with 6 columns.I want to append 'File1' as the 1 column to the file. i have the sample code .It is not working . can u please correct this or make new one ..... awk 'print {'File1',$1,$2,$3,$4,$5,$6}' Source_File> Result_File Source_File:... (6 Replies)
Discussion started by: satyam_sat
6 Replies

3. Shell Programming and Scripting

appending column file

Hi all, I have two files with the same number of lines the first file is a.dat and looks like 0.000 1.000 1.000 2.000 ... the fields are tab separated the second file is b.dat and looks like 1.2347 0.546 2.3564 0.321 ... the fields are tab separated I would like to have a file c.dat... (4 Replies)
Discussion started by: f_o_555
4 Replies

4. UNIX for Dummies Questions & Answers

Appending date value mmdd to first column in file

Hi , I have a file with a running sequence number. I need to append a date value mmdd format on to the first column. for e.g.: The file contains records as 001 abc 002 cde 003 edf 004 fgh 005 hik The output should be 1111001 abc 1111002 cde 1111003 edf 1111004 ... (1 Reply)
Discussion started by: kalyansid
1 Replies

5. Shell Programming and Scripting

Appending new column to existing files

Hi, i want to add another column to existing files containing strings and need to have the final output as a csv file. i have quite a number of files, each with varying number of rows and i need to append the string "test" for all the valid rows for each file. my sample raw files looks like this... (8 Replies)
Discussion started by: ida1215
8 Replies

6. UNIX for Dummies Questions & Answers

Appending a column of numbers in ascending order to a text file

I have a text file where I want to append a column of numbers in ascending orders. Input: 57 abc 25 def 32 ghi 54 jkl Output:57 abc 57 abc 1 25 def 2 32 ghi 3 54 jkl 4 How do I go about doing that? Thanks! (11 Replies)
Discussion started by: evelibertine
11 Replies

7. Shell Programming and Scripting

Appending column to rows

Hi All, Input.txt KGO Id "003" .......... .......... Par "CPara" BIN RECGET Name "DIR_PATH" Prompt "DIR_PATH" END RECGET KGO ............ .......... ............... KGO Id "077" .......... .......... (7 Replies)
Discussion started by: unme
7 Replies

8. Shell Programming and Scripting

Appending = in particular column in csv file

Hi, I have a requirement to append = in particular row in csv file. Data in csv is as follow: row1,a,a,a row2,b,b,b row3,c,c,c row4,d,d,d csv should be modified at row3 and no. of columns are not fixed but rows are. output should be as: row1,a,a,a row2,b,b,b row3,=c,=c,=c... (2 Replies)
Discussion started by: Divya1987
2 Replies

9. Shell Programming and Scripting

Python soap and string to .xlsx conversion

Hi experts - I'm relatively new to python, but I have an requirement to automate getting a file from a WebLib server using an API. The file I'm requesting from this sever is an excel spreadsheet (.xlsx). I get a valid response back via an xml doc from the server. In this xml file I get... (8 Replies)
Discussion started by: timj123
8 Replies

10. UNIX for Beginners Questions & Answers

How to insert data into black column( Secound Column ) in excel (.XLSX) file using shell script?

Source Code of the original script is down below please run the script and try to solve this problem this is my data and I want it column wise 2019-03-20 13:00:00:000 2019-03-20 15:00:00:000 1 Operating System LAB 0 1 1 1 1 1 1 1 1 1 0 1 (5 Replies)
Discussion started by: Shubham1182
5 Replies
All times are GMT -4. The time now is 11:15 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy