导入多个CSV文件并要求用户输入选择文件

目前可以说我有5个CSV文件。

*** File_A,File_B,File_C,File_D,File_E

它们以相同格式填充数据

列=日期,高,低,收盘价

jsj07123 回答:导入多个CSV文件并要求用户输入选择文件

如果您的问题是如何读取所选的选项,并且您知道可以打开哪些文件,那么我认为这段代码可能会对您有所帮助:

import pandas as pd

# List the possible files
files = ["fileA.csv","fileB.csv","fileC.csv"]

choose = 'z'
# Make a array with the options in the same order as the file array above
options = ['A','B','C']

# Ask the file to user until get a valid file
while choose not in options:
    choose = input("Choose one file (A,B or C): ")
    if choose not in options:
        print("No file {}. Try again!".format(choose))

df = pd.read_csv(files[options.index(choose)])
# Then do want you want if this data frame
,

我不确定我是否理解您的问题。但是请参见以下代码:

from glob import glob

files_csv = glob('*.csv')
if len(files_csv)==0:
    input("\n I can't find any csv file.")
elif len(files_csv)==1:
    file_csv = files_csv[0]
else:
    text = 'Choose file:\n\n'
    for i,a in enumerate(files_csv):
        text += '  {}) {}\n'.format(i+1,a)
    text +='\n--> '
    file_csv = files_csv[int(input(text))-1]
本文链接:https://www.f2er.com/3136190.html

大家都在问