[英]How to prompt the user for input once and for all before a while loop?
I'm working on a Python script which takes specific data from a pandas DataFrame called "read_file" and writes it in a csv file.我正在处理一个 Python 脚本,该脚本从 pandas DataFrame 中获取名为“read_file”的特定数据,并将其写入 csv 文件中。 To do so, I'm looping through the rows of the DataFrame until a certain condition is met, and that is when I put the data in a dictionary which I use to update the csv file by using the "to_csv" function. There are surely less broken - and simpler - ways to perform the same thing but I'm leaving optimization for another day (although I'm open to suggestions).
为此,我循环遍历 DataFrame 的行,直到满足特定条件,即当我将数据放入字典中时,我使用该字典通过使用“to_csv”function 更新 csv 文件。有执行相同操作的方法肯定不那么破损而且更简单,但我将在另一天进行优化(尽管我愿意接受建议)。
I would like to know if there is a way to prompt the user for a decision regarding the way they want to write the data.我想知道是否有办法提示用户就他们想要写入数据的方式做出决定。 Basically, I'm asking them whether they would like to:
基本上,我问他们是否愿意:
So far my code looks like this:到目前为止,我的代码如下所示:
i = 0
for index, row in read_file.iterrows():
case = row['Case']
first = case.split('-')[0]
second = case.split('-')[1]
third = case.split('-')[2]
fourth = case.split('-')[3]
fifth = case.split('-')[4]
if first == 'X01': # if1
if second == '01': # if2
if fourth == '04': # if3
i += 1
Ax = float(row['Ax'])
Ay = float(row['Ay'])
Az = float(row['Az'])
ENT = float(row['ENT'])
Ips = (Ax**2 + Ay**2 + Az**2)**(0.5)
beta = float(row['beta'])
date = row['Date'].replace("/", "-")
totalP = float(row['Total.P'])
data = pd.DataFrame({'Case': [str(case)],
'ENT': [ENT],
'total P': [totalP]},
index = [i])
filename = 'curve_{}-{}-{}-{}_I{}-B{}-D{}.csv'.format(first,second,third,fourth,round(Ips, 2),beta,date)
if os.path.getsize(filename):
print('Curve file not empty.')
while True:
inp = input('Do you want to: A) Append the file. B) Overwrite the file. [A/B]? : ')
if inp in ['A', 'B']:
break
if inp == 'A':
print('Appending... ')
data.to_csv(filename, mode='a')
elif inp == 'B':
print('Overwriting... ')
data.to_csv(filename, mode='w')
However this prompt appears each time the conditions if1, if2 & if3 are met, which is quite often.但是每次满足if1、if2 和 if3 条件时都会出现此提示,这是很常见的。 Is there a way to only specify the input once and for all?
有没有办法一劳永逸地只指定输入?
Thanks谢谢
EDIT:编辑:
A sample file would be:示例文件是:
Case![]() |
Date![]() |
ENT![]() |
beta![]() |
Ay![]() |
Ax![]() |
Az![]() |
Total.P![]() |
---|---|---|---|---|---|---|---|
X01-01-ARK-03-001 ![]() |
09/04/21 ![]() |
0 ![]() |
0,193648827 ![]() |
0 ![]() |
15 ![]() |
10 ![]() |
3354 ![]() |
X01-01-ARK-03-002 ![]() |
09/04/21 ![]() |
3 ![]() |
0,000183247 ![]() |
0 ![]() |
19 ![]() |
12 ![]() |
813 ![]() |
X01-01-ARK-03-003 ![]() |
09/04/21 ![]() |
6 ![]() |
0,491674971 ![]() |
0 ![]() |
14 ![]() |
0 ![]() |
4111 ![]() |
X01-01-ARK-03-004 ![]() |
09/04/21 ![]() |
9 ![]() |
0,757163602 ![]() |
0 ![]() |
1 ![]() |
5 ![]() |
2829 ![]() |
X01-01-ARK-03-005 ![]() |
09/04/21 ![]() |
12 ![]() |
0,622608381 ![]() |
0 ![]() |
44 ![]() |
11 ![]() |
143 ![]() |
X01-01-ARK-03-006 ![]() |
09/04/21 ![]() |
14 ![]() |
0,543299744 ![]() |
0 ![]() |
34 ![]() |
3 ![]() |
4732 ![]() |
X01-01-ARK-03-007 ![]() |
09/04/21 ![]() |
15 ![]() |
0,624404717 ![]() |
0 ![]() |
16 ![]() |
9 ![]() |
3052 ![]() |
X01-01-ARK-03-008 ![]() |
09/04/21 ![]() |
16 ![]() |
0,977021142 ![]() |
0 ![]() |
23 ![]() |
9 ![]() |
2178 ![]() |
X01-01-ARK-03-009 ![]() |
09/04/21 ![]() |
17 ![]() |
0,97697958 ![]() |
0 ![]() |
3 ![]() |
12 ![]() |
191 ![]() |
X01-01-ARK-04-001 ![]() |
09/04/21 ![]() |
0 ![]() |
0,440339098 ![]() |
0 ![]() |
33 ![]() |
9 ![]() |
1472 ![]() |
X01-01-ARK-04-002 ![]() |
09/04/21 ![]() |
3 ![]() |
0,982879346 ![]() |
0 ![]() |
49 ![]() |
2 ![]() |
253 ![]() |
X01-01-ARK-04-003 ![]() |
09/04/21 ![]() |
6 ![]() |
0,740821012 ![]() |
0 ![]() |
27 ![]() |
5 ![]() |
4100 ![]() |
X01-01-ARK-04-004 ![]() |
09/04/21 ![]() |
9 ![]() |
0,48267087 ![]() |
0 ![]() |
38 ![]() |
0 ![]() |
3582 ![]() |
X01-01-ARK-04-005 ![]() |
09/04/21 ![]() |
12 ![]() |
0,578068153 ![]() |
0 ![]() |
45 ![]() |
11 ![]() |
3320 ![]() |
X01-01-ARK-04-006 ![]() |
09/04/21 ![]() |
14 ![]() |
0,541974323 ![]() |
0 ![]() |
41 ![]() |
0 ![]() |
2064 ![]() |
X01-01-ARK-04-007 ![]() |
09/04/21 ![]() |
15 ![]() |
0,445777405 ![]() |
0 ![]() |
22 ![]() |
6 ![]() |
435 ![]() |
X01-01-ARK-04-008 ![]() |
09/04/21 ![]() |
16 ![]() |
0,263795323 ![]() |
0 ![]() |
27 ![]() |
10 ![]() |
1251 ![]() |
X01-01-ARK-04-009 ![]() |
09/04/21 ![]() |
17 ![]() |
0,708550336 ![]() |
0 ![]() |
44 ![]() |
12 ![]() |
60 ![]() |
and the expected output is:预期的 output 是:
index![]() |
Case![]() |
ENT![]() |
Total.P![]() |
---|---|---|---|
1 ![]() |
X01-01-ARK-04-001 ![]() |
0 ![]() |
1472 ![]() |
2 ![]() |
X01-01-ARK-04-002 ![]() |
3 ![]() |
253 ![]() |
3 ![]() |
X01-01-ARK-04-003 ![]() |
6 ![]() |
4100 ![]() |
4 ![]() |
X01-01-ARK-04-004 ![]() |
9 ![]() |
3582 ![]() |
5 ![]() |
X01-01-ARK-04-005 ![]() |
12 ![]() |
3320 ![]() |
6 ![]() |
X01-01-ARK-04-006 ![]() |
14 ![]() |
2064 ![]() |
7 ![]() |
X01-01-ARK-04-007 ![]() |
15 ![]() |
435 ![]() |
8 ![]() |
X01-01-ARK-04-008 ![]() |
16 ![]() |
1251 ![]() |
9 ![]() |
X01-01-ARK-04-009 ![]() |
17 ![]() |
60 ![]() |
Ask the user the question before you iterate over the file, this way the question is only asked (and answered) once:在遍历文件之前询问用户问题,这样问题只会被询问(并回答)一次:
while True:
inp = input('Do you want to: A) Append the file. B) Overwrite the file. [A/B]? : ')
if inp in ['A', 'B']:
break
i = 0
for index, row in read_file.iterrows():
case = row['Case']
first = case.split('-')[0]
second = case.split('-')[1]
third = case.split('-')[2]
fourth = case.split('-')[3]
fifth = case.split('-')[4]
if first == 'X01': # if1
if second == '01': # if2
if fourth == '04': # if3
i += 1
Ax = float(row['Ax'])
Ay = float(row['Ay'])
Az = float(row['Az'])
ENT = float(row['ENT'])
Ips = (Ax**2 + Ay**2 + Az**2)**(0.5)
beta = float(row['beta'])
date = row['Date'].replace("/", "-")
totalP = float(row['Total.P'])
data = pd.DataFrame({'Case': [str(case)],
'ENT': [ENT],
'total P': [totalP]},
index = [i])
filename = 'curve_{}-{}-{}-{}_I{}-B{}-D{}.csv'.format(first,second,third,fourth,round(Ips, 2),beta,date)
if os.path.getsize(filename):
print('Curve file not empty.')
if inp == 'A':
print('Appending... ')
data.to_csv(filename, mode='a')
elif inp == 'B':
print('Overwriting... ')
data.to_csv(filename, mode='w')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.