Thank you! That makes sense. So now when I print dict_pos, it seems to have formed a dictionary (not posting all of it as its large
But when writing to the excel worksheet, instead of writing the score, all values end up as 'Unknown_June2017'
Also, is it possible to form a dictionary with multiple keys ? For example, I need the first three columns in the 'score.txt' to be associated with the score value and that needs to be compared with column 5,6,7 from the worksheet
...But when writing to the excel worksheet, instead of writing the score, all values end up as 'Unknown_June2017'
Also, is it possible to form a dictionary with multiple keys ? For example, I need the first three columns in the 'score.txt' to be associated with the score value and that needs to be compared with column 5,6,7 from the worksheet
My hunch is that you are looking at the wrong column.
If your "pos_col_no" is "F" and "row_no" is 4, then the code will look at cells F4, F5, F6, F7, F8, .... and check if they are keys of dictionary "dpos".
Since you see 'Unknown_June2017' in cells V4, V5, V6, V7, V8, ... it means that the keys are not in column F but in some other column.
Yes, it's possible to form a dictionary with multiple keys.
You can use a special Python data structure called a "tuple" for that.
Elements of tuples have parentheses around them e.g.
is a tuple.
The code can work without parentheses (for the most part) but it's better to specify them in order to avoid ambiguity.
Like so:
Comparing tuples is easy:
But this is where parentheses are important:
This User Gave Thanks to durden_tyler For This Post:
Thank you.
yes, i was using the wrong column! Now this is my final code
when I run it now, it doesnt print the cell.value. So when I'm attempting to do is making the code to compare the threee columns in the excel file to the three columns in the text file so that it can output its corresponding Score
Last edited by nans; 06-27-2017 at 09:35 AM..
Reason: updated code
I haven't gone through the entire code yet, and it might be tricky to test your code since I don't have pandas, but let me answer your question about the function call.
Quote:
Originally Posted by nans
...
...
...
The replace() function replaces the value '.xlsx' to '.txt' in the string variable sheet_file.
The string variable sheet_file holds the name of your Excel file.
So, let's say while looping through the sheet_directory, your Python program finds an Excel file called "S12.xlsx". Then sheet_file will equal "S12.xlsx".
Thereafter, this expression:
replaces '.xlsx' to '.txt' and thereby returns 'S12.txt'.
And then this value 'S12.txt' is passed to the function get_text_data().
That is, the value of the string parameter txt_filename is 'S12.txt'.
You can see this very quickly by printing txt_filename the moment you enter the function.
In the "with" statement inside the function "get_text_data", however, you use the same name txt_filename. That converts the string parameter txt_filename to a file object.
Thereafter, till the end of the function "get_text_data", txt_filename remains a file object.
So essentially, you are not using the txt_filename parameter in your function at all.
My suggestion: don't pass a parameter to a function if you are not using it at all. You anyway have the text file name ("scores.txt") and location hard-coded.
If something is not needed, discard it. Keep it simple.
This User Gave Thanks to durden_tyler For This Post:
...
If I remove that, then what would be the best way to proceed further
...
1) Remove the parameter, not the function call. In other words, do call the function, but don't pass any parameter to it.
2) Change the signature of the function so that it does not accept any parameter.
3) Call the function only once. Calling the same function that reads the same text file and returns the same dictionary every time you find an Excel spreadsheet is extremely inefficient.
4) Check what you are trying to print.
So,
There is no cell called "EGF4" in any Excel spreadsheet.
Hence Python cannot print it.
>>> Ok, that's a wrong statement. My bad, sorry. Looks like newer versions of Microsoft Excel do have cell "EGF4".
>>> That may not be the cell you want to print. My guess is that your program is printing "None" instead of nothing.
>>> Your spreadsheet's cell "EGF4" is empty, most likely.
Last edited by durden_tyler; 06-27-2017 at 06:15 PM..
This User Gave Thanks to durden_tyler For This Post:
1) Remove the parameter, not the function call. In other words, do call the function, but don't pass any parameter to it.
2) Change the signature of the function so that it does not accept any parameter.
3) Call the function only once. Calling the same function that reads the same text file and returns the same dictionary every time you find an Excel spreadsheet is extremely inefficient.
4) Check what you are trying to print.
So,
There is no cell called "EGF4" in any Excel spreadsheet.
Hence Python cannot print it.
>>> Ok, that's a wrong statement. My bad, sorry. Looks like newer versions of Microsoft Excel do have cell "EGF4".
>>> That may not be the cell you want to print. My guess is that your program is printing "None" instead of nothing.
>>> Your spreadsheet's cell "EGF4" is empty, most likely.
I'm using Libre Office, an older version, not microsoft excel. So what would be the appropriate way of calling E4:G4:F4 -> V4
What do you mean by point 2 ? Will.it then be
dpos = get_text_data(txt_filename)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)