Python:为了不同的进程循环一定的时间

时间:2015-03-24 05:01:14

标签: python if-statement tkinter

我创建了一个脚本,假设使用Tkinter来允许选择和加载文件并将其内容存储在不同的对象中,然后处理这些文档中的每一个。 我想让脚本只能处理由问题确定的一定数量的文件(该值存储在“File_number”下) 例如:如果问题是“你要比较多少个文件?” 用户输入3 我想tkinter openfile窗口只询问3个文件然后继续

我正在使用如下所示的If Else语句 但它似乎不能很好地工作,代码实际上不是pythonic。 是否有更好/更短的方式来执行相同的操作?

由于

我的脚本看起来像这样

import pandas as pd
from pandas import *
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import seaborn as sns
import pylab
import pandas.io.data
import os
import Tkinter
from Tkinter import *
import tkFileDialog
import tkSimpleDialog
from tkFileDialog import askopenfilename
import sys

# Set up GUI
root = Tkinter.Tk(); root.withdraw()

# Prompt for user info
File_number = tkSimpleDialog.askinteger("File number", "How many files do you want to compare?")


# Prompt for file explorer
# Also extract the file_name

process_a = 0

if process_a = File_number:
    break
else:
    process_a = process_a + 1
    fileloc1 = tkFileDialog.askopenfilename(parent=root, title='Choose file 1')
    fileloc1_name_clean = os.path.splitext(fileloc1)[0]
    fileloc1_name = os.path.basename(fileloc1_name_clean)

if process_a = File_number:
    break
else:
    process_a = process_a + 1
    fileloc2 = tkFileDialog.askopenfilename(parent=root, title='Choose file 2')
    fileloc2_name_clean = os.path.splitext(fileloc2)[0]
    fileloc2_name = os.path.basename(fileloc2_name_clean)

if process_a = File_number:
    break
else:
    process_a = process_a + 1
    fileloc3 = tkFileDialog.askopenfilename(parent=root, title='Choose file 3')
    fileloc3_name_clean = os.path.splitext(fileloc3)[0]
    fileloc3_name = os.path.basename(fileloc3_name_clean)
编辑1
The next part of my script is:

dfa_1 = pd.read_csv(fileloc1, delimiter='\t')
dfa_nodupli = dfa_1.drop_duplicates(cols='N', take_last=False)    
dfa_nodu_2pep = dfa_nodupli[(dfa_nodupli['Peptides(95%)'] > 1)]   
dfa_nodu_2pep = dfa_nodu_2pep[~dfa_nodu_2pep['Name'].str.contains('Keratin')]
dfa_nodu_2pep.to_csv(fileloc1_name + ".csv")



dfb_1 = pd.read_csv(fileloc2, delimiter='\t')
dfb_nodupli = dfb_1.drop_duplicates(cols='N', take_last=False)    
dfb_nodu_2pep = dfb_nodupli[(dfb_nodupli['Peptides(95%)'] > 1)]   
dfb_nodu_2pep = dfb_nodu_2pep[~dfb_nodu_2pep['Name'].str.contains('Keratin')]
dfb_nodu_2pep.to_csv(fileloc2_name + ".csv")

1 个答案:

答案 0 :(得分:2)

我修改了你的代码,以便它以你想要的方式工作(我希望)。

import Tkinter
import tkFileDialog
import tkSimpleDialog
from tkFileDialog import askopenfilename
import os

# Set up GUI


def main():

    root = Tkinter.Tk(); 
    root.withdraw()

    # Prompt for user info
    File_number = tkSimpleDialog.askinteger("File number", 
                                            "How many files do you want to compare?")

    if not File_number:
        return

    user_fiels = []


    max_file_no = int(File_number)
    current_file = 1;

    while(current_file <= max_file_no):

        fileloc = tkFileDialog.askopenfilename(parent=root, title='Choose file {}'.format(current_file))

        if not fileloc:
            continue

        fileloc_name_clean = os.path.splitext(fileloc)[0]
        fileloc_name = os.path.basename(fileloc_name_clean)

        user_fiels.append([fileloc, fileloc_name_clean, fileloc_name])

        current_file += 1

        #print(fileloc_name_clean, fileloc_name)

    print(user_fiels) 

main()

我使用while循环来根据需要多次获取文件路径。