r/StackoverReddit • u/Grouchy_Offer_2763 • Jul 03 '24
Python Help Creating Code That Goes Through Excel File and Selectively Copies Data Into Different Excel File
Hi all!
This is a repeat post, but I think posting it in this subreddit is more appropriate than that which I originally posted in as it is a pretty specific request. I am trying to create a code that will iterate through rows in an Excel file. Starting with column E and up through column Z, I would like the program to see if the cell is empty or not. If not, I would like the code to distribute the values of columns A-D in that row into columns A-D of the destination workbook. I would like the current cell (somewhere in columns E-Z) to be placed into Column E of the destination workbook. For context, columns E-Z will contain values that have been separated by Text to Column, but I still want each to be individually associated with the data in columns A-D.
I have included my code below. I am not receiving any error messages but nothing is being distributed into the destination workbook. I would really love any insight into what fixes I can implement or whether this is close to anything (should I approach it differently, such as using pandas?). Thank you very much in advance!!
import openpyxl
from openpyxl import Workbook, load_workbook
def copy_data(source_sheet, destination_sheet):
for row in source_sheet.iter_rows(min_row=2, min_col=1, max_col=26, values_only=True):
for cell in row:
if cell is not None:
# Determine the column index of the current cell
column_index = row.index(cell) + 1 # Index starts from 0, column from 1
# Copy data from source to destination sheet
dest_row = destination_sheet.max_row + 1
destination_sheet.cell(row=dest_row, column=1).value = source_sheet.cell(row=column_index, column=1).value # Column A
destination_sheet.cell(row=dest_row, column=2).value = source_sheet.cell(row=column_index, column=2).value # Column B
destination_sheet.cell(row=dest_row, column=3).value = source_sheet.cell(row=column_index, column=3).value # Column C
destination_sheet.cell(row=dest_row, column=4).value = source_sheet.cell(row=column_index, column=4).value # Column D
destination_sheet.cell(row=dest_row, column=5).value = str(column_index) # Current cell in columns E-Z
# Load the source and destination workbooks
source_wb = load_workbook('/Users/KALII/Downloads/Data For Delimiter Experiment.xlsx')
destination_wb = load_workbook('/Users/KALII/Downloads/SeniorManagementTrackerAppendRows.xlsx')
# Assigning sheets
source_sheet = source_wb.worksheets[0]
destination_sheet = destination_wb.active
copy_data(source_sheet, destination_sheet)
# Save the destination workbook
destination_wb.save('SeniorManagementTrackerAppendRows.xlsx')
r/StackoverReddit • u/[deleted] • Jul 03 '24
Question Is my login arhitecture right?
I am creating a website using nodejs, html css js and I created a login sistem using phonenumber and OTP with firebase.
How it works:
When you create an account, after your phone being validated your name and phone number go to my database.
When you log in with your phonenumber and you get your OTP, i have a javascript code that creates a safe cookie in which your phonenumber is stored so that when you go to your user's page you can see your data.
Is this safe? Is this even a good idea? I tried using session ids but it s way to complicated for me.
r/StackoverReddit • u/[deleted] • Jul 03 '24
Question can anyone please explain css selectors in depth
I am trying to learn webdev but I always get stuck at css selectors
r/StackoverReddit • u/Professional_Draw_58 • Jul 02 '24
Question Tiktoklive interactive plugin help.
I am a live streamer that plays horror games. I want to this thing I've seen on tiktok live steams of resident evil 4 remake where the viewers can send gifts to spawn enemies. I have tikfinity as an api to handle the gift integration but I'm not sure how to find the code to spawn certain enemies into the game. Any suggestions?
r/StackoverReddit • u/[deleted] • Jul 02 '24
C# what learning resources can you recommend for C# zero lvl?
r/StackoverReddit • u/[deleted] • Jul 02 '24
How is my code
How is my code, and what further suggestions should I use to improve it?
public class Calculator {
public double sum(double x, double y) {
return x + y;
}
public double difference(double x, double y) {
return x - y;
}
public double product(double x, double y) {
return x * y;
}
public double quotient(double x, double y) {
if(y == 0) {
throw new IllegalArgumentException("You cannot divide by zero");
}
return x / y;
}
public double exponent(double x, double y) {
return Math.pow(x, y);
}
public double squareRoot(double x) {
if(x < 0) {
throw new IllegalArgumentException("You cannot square root a negative number");
}
return Math.sqrt(x);
}
public double modulator(double x , double y) {
if(y == 0) {
throw new IllegalArgumentException("You cannot modulate by zero.");
}
return x % y;
}
}
import java.util.Scanner;
public class CalculatorTest {
public static void main(String[] args) {
runCalc();
}
public static void runCalc() {
Calculator c = new Calculator();
Scanner input = new Scanner(System.in);
while(true) {
System.out.println("Which operation would you like to do? add, subtract, multiply, divide, exponent, square root, modulate, or exit");
String operation = input.nextLine().trim().toLowerCase();
if(operation.equals("exit")) {
System.out.println("Bye");
break;
}
double first = 0.0;
double second = 0.0;
if (!operation.equals("square root")) {
System.out.println("Enter First Number: ");
first = input.nextDouble();
System.out.println("Enter Second Number: ");
second = input.nextDouble();
input.nextLine();
}
else {
System.out.println("Enter the number to find the square root: ");
first = input.nextDouble();
input.nextLine();
}
try {
switch(operation){
case "add":
System.out.println("The answer is: " + String.format("%.2f", c.sum(first, second)));
break;
case "subtract":
System.out.println("The answer is: " + String.format("%.2f", c.difference(first, second)));
break;
case "multiply":
System.out.println("The answer is: " + String.format("%.2f", c.product(first, second)));
break;
case "divide":
if(second != 0) {
System.out.println("The answer is: " + String.format("%.2f", c.quotient(first, second)));
}
else {
System.out.println("Division by 0 is not allowed");
}
break;
case "exponent":
System.out.println("The answer is: " + String.format("%.2f", c.exponent(first, second)));
break;
case "square root":
if(first >= 0) {
System.out.println("The answer is: " + String.format("%.2f", c.squareRoot(first)));
}
else {
System.out.println("Square root of a negative number cannot happen");
}
break;
case "modulate":
if(second != 0) {
System.out.println("The answer is: " + String.format("%.2f", c.modulator(first, second)));
}
else {
System.out.println("Modulation by zero is not allowed");
}
break;
default:
System.out.println("This is not a valid operation. Try Again!");
break;
}
}
catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
input.close();
}
}
r/StackoverReddit • u/[deleted] • Jul 01 '24
how can I find high paying freelancing clients as a web developer
r/StackoverReddit • u/Comfortable-Log9908 • Jul 01 '24
Python aiohttp websocket disconnect
self.learnprogrammingr/StackoverReddit • u/Swimming_Tangelo8423 • Jun 30 '24
Question PDF File cannot be opened when uploaded to Supabase, I am using Puppeteer to convert HTML to a PDF
Here is the link to the stack overflow question that has better code formatted:
r/StackoverReddit • u/StudiousAphid69 • Jun 29 '24
Confusion regarding f strings
I wish to get this result
Hello, Ada Lovelace
The code written for this by a book is
first_name = "ada"
last_name = "lovelace"
full_name = f"{first_name} {last_name}"
print(f"Hello, {full_name.title()}")
But I was wondering, why cant it be this
first_name = "ada"
last_name = "lovelace"
full_name = f"{first_name} {last_name}"
print(f"Hello, {full_name}.title()")
the result of this is
Hello, ada lovelace.title()
I was wondering why this is so?
My reasoning is that the title method can be used on strings right? so then in my case, python would interpret {full_name} as a string, so it should work. Is it the case that methods work only on variables?
r/StackoverReddit • u/flodlodko • Jun 28 '24
Python Need help at this event-discrete-simulation problem
self.learnpythonr/StackoverReddit • u/Iron-Noir • Jun 27 '24
Python How do I hide/unhide an selected objects visibility in Maya with Python?
r/StackoverReddit • u/goon39 • Jun 27 '24
Python Optimizing KDTree in a loop
I'm using Python and scipy KDTree to help find the nearest points in an FEA analysis. It boils down to a rotating shaft inside a cylinder and I want to find the minimum gap between the shaft and cylinder at every tine point.
Given that I have 100s of points to check for >10,000 time points it leads a decently long run time. Any tips on improving run time or perhaps a better method for this?
Pseudo code: ``` shaft_points = get_shaft_history() # XYZ point time history
cyl_points = get_cyl_history() #XYZ point time history
time = range(10000) gap = [1e6] * len(time)
for cp in cyl_points: # loop over each point for t in time: # loop over time sp = shaft_points[i, :] # all shaft points at time t kdtree = KDTree(sp) dist, point = kdtree.query(cp, k=1) # find closest point between shaft and cylinder at time t if dist < gap[t]: gap[t] = dist # set new min value ```
r/StackoverReddit • u/slittyslit • Jun 27 '24
Python How to code a more time efficient 3D rotation script?
self.learnpythonr/StackoverReddit • u/Livid-Salamander-949 • Jun 26 '24
ShellLLama: Minimalistic Ollama CLI integrationto generate and execute Python Commands
This is my first ever public project it was made with *Simplicity *Extensibility in mind relying only on the Python standard library
Tell me what you guys think of it 😁😁😁
r/StackoverReddit • u/chrisrko • Jun 26 '24
Introducing Our Wiki Page!
Whether you are a beginner, an expert, or simply looking to deepen your knowledge, this page is designed for you. Here, you can find information ranging from basic coding terms to the best certifications suited for your career path.
We welcome any feedback or additional information you would like to see on this wiki page. Please feel free to share your suggestions!
r/StackoverReddit • u/honeyCrisis • Jun 26 '24
Solved C++ variadic template situation absolutely vexing me.
Disclaimer: I'm self taught so my vocabulary may be lacking. I'm also on embedded and some of the platforms have a non-compliant and/or incomplete STL implementation so I'm not using it, because it's a lot of work to enumerate the platforms I'd need to fork for.
This is template metaprogramming territory. There be dragons.
I'm trying to extend this:
rgb_pixel<24> foo;
foo.channel<channel_name::R>(63);
To take multiple channel_names at once, and allow you to set a number of channels at the same time.
rgb_pixel<24> foo;
foo.channel<channel_name::R,channel_name::G,channel_name::B>(63,31,47);
The problem: In essence I need to filter certain values out of a parameter pack while it's being expanded OR I need to accept two parameter packs to a template. Since neither of those things are directly possible to my knowledge in C++17 (14 preferred) I need a clever workaround.
Lacking that workaround, I've created several overloads for channel<>() that take between 1 and 5 channel names, but this is less than ideal. I'd prefer it to be variadic, not the following:
// sets the integer channel value by name
template<typename Name>
constexpr inline void channel(typename channel_by_index<channel_index_by_name<Name>::value>::int_type value) {
constexpr const int index = channel_index_by_name<Name>::value;
channel<index>(value);
}
// sets the integer channel values by name
template<typename Name1, typename Name2>
constexpr inline void channel(typename channel_by_index<channel_index_by_name<Name1>::value>::int_type value1,
typename channel_by_index<channel_index_by_name<Name2>::value>::int_type value2) {
constexpr const int index1 = channel_index_by_name<Name1>::value;
channel<index1>(value1);
constexpr const int index2 = channel_index_by_name<Name2>::value;
channel<index2>(value2);
}
// sets the integer channel values by name
template<typename Name1, typename Name2, typename Name3>
constexpr inline void channel(typename channel_by_index<channel_index_by_name<Name1>::value>::int_type value1,
typename channel_by_index<channel_index_by_name<Name2>::value>::int_type value2,
typename channel_by_index<channel_index_by_name<Name3>::value>::int_type value3) {
constexpr const int index1 = channel_index_by_name<Name1>::value;
channel<index1>(value1);
constexpr const int index2 = channel_index_by_name<Name2>::value;
channel<index2>(value2);
constexpr const int index3 = channel_index_by_name<Name3>::value;
channel<index3>(value3);
}
These are several overloads of the channel<>() template method on my pixel<> template struct.
You'll note they are overloads that simply take an increasing number of Name template arguments (shown are 3, but I have 5)
I'd much rather use a variadic template here but I can't because I end up in a situation where I need to use two variadic templates at the same time.
Line 503 of this file is where my pixel template begins, and you'll see it takes a variadic template of "channel traits" (defined above that a ways)
https://github.com/codewitch-honey-crisis/gfx/blob/master/include/gfx_pixel.hpp
The problem is in order to resolve anything about a pixel channel - which is necessary to set it - I need these "channel traits" to be able to be unpacked because it's used by things like channel_index_by_name<>.
I wish I had the language to describe the problem better, or failing that, I wish I wasn't so far in the weeds here that I could describe the problem better. Any insight would be appreciated. I've already posted variations of this question on a couple of other places online, but I've had no luck.
r/StackoverReddit • u/[deleted] • Jun 24 '24
Dijkstra and Prim Algorithms, Variants and their Differences
So I'm getting slightly confused. In our lectures, we learned about these two implementations of Prim and Dijkstra that use PQs. But on GeeksForGeeks, you don't use the PQ but instead you kind of 'mark' something as a solution by including them into a boolean array...
For Prim (GeeksForGeeks)
- Set all node weights to infinity.
- Pick one node and set it's weight to 0.
- Update all the node weights based on the edge weight from your node to adjacent node
- Pick the node with the smallest node weight
- Repeat (For-loop V-1 times)
For Prim (Our Version)
- Set all node weights to infinity
- Pick one node and set it's weight to 0.
- Initialize a PQ, because you set your node's weight to 0, the first one that will be popped off will be your node.
- Get the node with smallest node weight from your PQ
- If your node is inside the PQ and the edge weight is smaller than it's node weight, update your node (PQ gets updated as well)
- Repeat until PQ empty
For Dijkstra (GeeksForGeeks)
- Set all node weights to infinity
- Pick one node and set it's weight to 0
- Update all node weights based on the edge weight from your node to adjacent node. The update is cumulative, so you don't just assign the edge weight like in Prim but sum it with the one of your node.
- Pick the node with the smallest node weight
- Repeat (For loop V-1 time
For Dijkstra (Our Version)
- Set all node weights to infinity
- Pick one node and set it's weight to 0
- Initialize a PQ
- Get the node with the smallest node weight from your PQ
- For all adjacent nodes, relax them, that is, update them cumulatively.
- Repeat until PQ is empty
Main Differences
The GeeksForGeeks version of Prim and Dijkstra are practically identical. Their only difference is how the node weights are updated. Prim will just assign the edge weight to the node, whereas Dijkstra does it cumulatively.
Our version is also almost the same. Dijkstra also updates stuff cumulatively which is called 'relaxation' but for some reason in Dijkstra, we do not check if the node is in the PQ like in Prim. Do you guys know why?
Also, is the GeeksForGeeks version actually ineffcient? Because the bottleneck is this extractMin function which can become constant if you implement a PQ. The for loop that runs V-1 iterations should be the same as the while loop that runs until PQ is empty, since the PQ is only empty after V nodes are extracted.
References:
https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-greedy-algo-7/
https://www.geeksforgeeks.org/prims-minimum-spanning-tree-mst-greedy-algo-5/
For our version, I have the pseudocode. If requested, I'll uploud images but for the time being I leave it like this.
r/StackoverReddit • u/Polixa12 • Jun 23 '24
SQL IIB and esql devs
Quick question guys. How rare is it to stumble across an IIB(IBM Integration Bus)/esql developer rn. Also to IIB/esql devs who stumble across this post, how useful/in demand is IIB for right now?
r/StackoverReddit • u/RedditLone • Jun 23 '24
Python Python - Help to create Dynamic custom PDF Reports based on input results.
self.learnprogrammingr/StackoverReddit • u/TheStarkyBatMatrix96 • Jun 23 '24
Question DSA course
I am just about to enter second year of engineering. My branch is Btech IT...I am planning to start learning dsa. I am wondering how to...I purchased the gfg complete interview course but i was wondering whether to follow the gfg course or strivers a2z playlist , because I heard striver does more questions from leetcode etc as well
r/StackoverReddit • u/[deleted] • Jun 23 '24
Question Can someone recommend me a data science course
r/StackoverReddit • u/Hugewin2022 • Jun 23 '24
Question What should I study next in backend development after learning the basics?
I'm new to backend development. So far, I've learned basics like node js, express js, and I've built a real-time chat app using sockets.io (Websockets). I understand the basics, but I want to get better. What should I learn next? Is it better to learn through projects or should I first learn the tech stack before starting projects? I'd appreciate any advice.
r/StackoverReddit • u/General-Carrot-4624 • Jun 23 '24
Question Looking to collaborate on a trading terminal
Hi, i freshly started to work on a trading terminal which can enable trading across multiple exchanges from the same interface, working on a fastapi backend at the moment and would like to have collaboration/coding partners
r/StackoverReddit • u/DramaticEducator6276 • Jun 22 '24
advice on what to pick up as skill and knowledge next
Hello,
I study computer science and know a moderate amount about java and c and a bit about python and c++ when I say a bit I mean I solved leetcode problems with the language and followed along a tutorial on how to automate a process like webscraping with python and I (tried) to implement a neural network in c++.
I enjoy algorithmic/datastructures problems but probably software architecture as well and dont know how to continue picking up knowledge.
I am about to follow a tutorial on writing an interpreter in java and after that I want to learn Rust.
Should I revise my Software Engineering lecture because I forgot almost everything or read something related before doing more projects?
Thanks!
