简体   繁体   English

如何在 while 循环之前一劳永逸地提示用户输入?

[英]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:基本上,我问他们是否愿意:

  1. Overwrite the file and then update it row by row (each time we go through the loop) via the append mode of the to_csv function覆盖文件,然后通过to_csv function的append模式逐行更新(每次我们go通过循环)
  2. Not overwrite the file and just append data to it each time the script is run (this means it's getting bigger after a run).每次运行脚本时不覆盖文件,只覆盖 append 数据(这意味着运行后它会变大)。

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总P
X01-01-ARK-03-001 X01-01-方舟-03-001 09/04/21 21/09/04 0 0 0,193648827 0,193648827 0 0 15 15 10 10 3354 3354
X01-01-ARK-03-002 X01-01-方舟-03-002 09/04/21 21/09/04 3 3个 0,000183247 0,000183247 0 0 19 19 12 12 813 813
X01-01-ARK-03-003 X01-01-方舟-03-003 09/04/21 21/09/04 6 6个 0,491674971 0,491674971 0 0 14 14 0 0 4111 4111
X01-01-ARK-03-004 X01-01-方舟-03-004 09/04/21 21/09/04 9 9 0,757163602 0,757163602 0 0 1 1个 5 5个 2829 2829
X01-01-ARK-03-005 X01-01-方舟-03-005 09/04/21 21/09/04 12 12 0,622608381 0,622608381 0 0 44 44 11 11 143 143
X01-01-ARK-03-006 X01-01-方舟-03-006 09/04/21 21/09/04 14 14 0,543299744 0,543299744 0 0 34 34 3 3个 4732 4732
X01-01-ARK-03-007 X01-01-方舟-03-007 09/04/21 21/09/04 15 15 0,624404717 0,624404717 0 0 16 16 9 9 3052 3052
X01-01-ARK-03-008 X01-01-方舟-03-008 09/04/21 21/09/04 16 16 0,977021142 0,977021142 0 0 23 23 9 9 2178 2178
X01-01-ARK-03-009 X01-01-方舟-03-009 09/04/21 21/09/04 17 17 0,97697958 0,97697958 0 0 3 3个 12 12 191 191
X01-01-ARK-04-001 X01-01-方舟-04-001 09/04/21 21/09/04 0 0 0,440339098 0,440339098 0 0 33 33 9 9 1472 1472
X01-01-ARK-04-002 X01-01-方舟-04-002 09/04/21 21/09/04 3 3个 0,982879346 0,982879346 0 0 49 49 2 2个 253 253
X01-01-ARK-04-003 X01-01-方舟-04-003 09/04/21 21/09/04 6 6个 0,740821012 0,740821012 0 0 27 27 5 5个 4100 4100
X01-01-ARK-04-004 X01-01-方舟-04-004 09/04/21 21/09/04 9 9 0,48267087 0,48267087 0 0 38 38 0 0 3582 3582
X01-01-ARK-04-005 X01-01-方舟-04-005 09/04/21 21/09/04 12 12 0,578068153 0,578068153 0 0 45 45 11 11 3320 3320
X01-01-ARK-04-006 X01-01-方舟-04-006 09/04/21 21/09/04 14 14 0,541974323 0,541974323 0 0 41 41 0 0 2064 2064
X01-01-ARK-04-007 X01-01-ARK-04-007 09/04/21 21/09/04 15 15 0,445777405 0,445777405 0 0 22 22 6 6个 435 435
X01-01-ARK-04-008 X01-01-方舟-04-008 09/04/21 21/09/04 16 16 0,263795323 0,263795323 0 0 27 27 10 10 1251 1251
X01-01-ARK-04-009 X01-01-方舟-04-009 09/04/21 21/09/04 17 17 0,708550336 0,708550336 0 0 44 44 12 12 60 60

and the expected output is:预期的 output 是:

index指数 Case案件 ENT耳鼻喉科 Total.P总P
1 1个 X01-01-ARK-04-001 X01-01-方舟-04-001 0 0 1472 1472
2 2个 X01-01-ARK-04-002 X01-01-方舟-04-002 3 3个 253 253
3 3个 X01-01-ARK-04-003 X01-01-方舟-04-003 6 6个 4100 4100
4 4个 X01-01-ARK-04-004 X01-01-方舟-04-004 9 9 3582 3582
5 5个 X01-01-ARK-04-005 X01-01-方舟-04-005 12 12 3320 3320
6 6个 X01-01-ARK-04-006 X01-01-方舟-04-006 14 14 2064 2064
7 7 X01-01-ARK-04-007 X01-01-ARK-04-007 15 15 435 435
8 8个 X01-01-ARK-04-008 X01-01-方舟-04-008 16 16 1251 1251
9 9 X01-01-ARK-04-009 X01-01-方舟-04-009 17 17 60 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM