Sponsored Content
Top Forums Programming Appending a column in xlsx file using Python Post 303000049 by durden_tyler on Monday 3rd of July 2017 11:56:54 AM
Old 07-03-2017
Quote:
Originally Posted by nans
Correct, those values are of (E4, G4, F4). But with "row_no += 1", shouldnt it pick up the values from the next row ?
...
...
No, it should not.
It will not.
No programming language will do that for you.
Golden rule of programming => a programming language will only do what you tell it to do. It will not do anything on its own.

Quote:
Originally Posted by nans
...
I thought this while loop meant 'go to row 4, if the row matches the score from the dictionary, print the value or else print unknown and then go on to the next row and do the same'
...
The "while" loop is only for repeating a "set of actions" while a "condition" is true.

The repetitive "set of actions" is:
1) going to next row
2) determining pos value, alt value, ref value for the row we reached
3) constructing the cpos value from the 3 values determined in the step above
4) checking if this cpos value is in the dictionary dict_pos
5) setting the value of "score" cell in the row that we are now

Since 1) through 5) are repetitive, we work on the entire spreadsheet, but one row at a time.
Inside a "while" loop, we are only working on one row - the current row.

The "condition" is: either pos value is non-empty or alt value is non-empty or ref value is non-empty.

As you can see, the structure of a "while" loop is very generic (repeat a set of actions while a condition is true), so it can be used in a wide variety of situations in any programming language.

Quote:
Originally Posted by nans
...
...
How would I recalculate the values in a loop for all rows ?

Code:
                while row_no <=10:
                    if (dict_pos.has_key(cpos)):
                        NG = dict_pos[cpos]
                        #print dict_pos[cpos]
                    else:
                        NG = 'Unknown_' + datetime.now().strftime("%B") + datetime.now().strftime("%Y")
                    row_no += 1
                    print row_no
                    cpos = Scores(POS=cell_pos.value, ALT=cell_alt.value, REF=cell_ref.value)
                    print cpos
                wb.save(sheet_xl_file)

...
...
Old proverb: "How to eat an elephant? One bite at a time." Smilie

As I mentioned earlier, inside a loop, you only have to calculate the pos, alt and ref values for one row - the current row, the row you are on.

Now, for row_no = 4, you had a few statements that calculated cell_pos, cell_alt and cell_ref. Then you calculated "cpos" from those cell_pos, cell_alt and cell_ref values.

You need those statements inside the "while" loop.
So once the row_no is incremented inside the loop, you:
1) determine the cell_pos, cell_alt and cell_ref
2) next you calculate the cpos using cell_pos, cell_alt and cell_ref
This will be your "cpos" - only for the current row.
When printed, you will see different values of cpos - printed one at a time from within your "while" loop.

(The output that I showed in my earlier post was not printed in one shot - it was printed during each iteration of the loop - one line printed per iteration.)

Last edited by durden_tyler; 07-03-2017 at 01:05 PM..
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 09:39 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy