I have a python script that will ultimately be converted into an .exe
file. The way it is supposed to work is that a user will drag an excel file and drop it on to the python file, which will run, and create another output excel file.
However, it needs 2 inputs from the user: groups
and size
.
When I drop the excel file, the command prompt opens up automatically, and prompts me for the inputs. However, after submitting the values, the command prompt closes automatically without executing the rest of the script.
The problem goes away if I hard-code the values for groups
and size
without prompting the user.
Here is the code:
import pandas as pd
import sys
x = sys.argv[1]
file = pd.read_excel(x)
file = file.sample(frac=1).reset_index(drop=True)
file['Order'] = file.groupby('GENDER').cumcount()
file = file.sort_values(['Order', 'GENDER'], ascending=[True,False]).reset_index(drop=True)
groups = input ("Group number?")
size = input ("Group size?")
out = {}
count = 1
students = len(file)
std_sorted = 0
for i in range (0, len(file)-size, size):
if count != groups:
out["group"+str(count)] = file[i:(i+size)]
else:
out["group"+str(count)] = file[i:]
count += 1
std_sorted += size
if std_sorted != students:
out["group"+str(count)] = file[std_sorted:]
index = 0
writer = pd.ExcelWriter('output.xlsx')
for i in out:
out[i].pop("Order")
x = pd.DataFrame(out[i])
x.to_excel(writer, index = False, sheet_name="Sheet"+str(index))
workbook = writer.book # Access the workbook
worksheet= writer.sheets["Sheet"+str(index)] # Access the Worksheet
header_list = x.columns.values.tolist() # Generate list of headers
for i in range(0, len(header_list)):
worksheet.set_column(i, i, len(header_list[i])+2) # Set column widths based on len(header)
index += 1
writer.save()
I could find your problem just from copy-pasting your code to Pycharm without even running it (Pycharm does a great job of code inspection)...
The line:
for i in range (0, len(file)-size, size):
will always raise a TypeError
exception. This happens because len(...)
is an int, BUT size
is a string because it's an input. You need to change your inputs to:
groups = int(input("Group number?"))
size = int(input("Group size?"))
Try
groups = int(input ("Group number?")) size = int(input ("Group size?"))
instead of
groups = input ("Group number?") size = input ("Group size?")
I think it a typecasting issue. Try it maybe it fixes your issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.