Python csv文件查找

时间:2018-03-13 23:10:14

标签: python csv

处理我们已经提供了三个csv文件的项目(按城市名称)。项目需要编写交互式代码,如果用户指定城市,则显示城市的完整路径名。

def get_city():
    '''Asks the user for a city and returns the filename for that city's bike share data.

    Args:
        none.
    Returns:
        (str) Filename for a city's bikeshare data.
    '''
    city = input('\nHello! Let\'s explore some US bikeshare data!\n'
                 'Would you like to see data for Chicago, New York, or Washington?\n')

1 个答案:

答案 0 :(得分:0)

我假设你有一个特定的文件夹或文件夹列表,其中存储了所有文件。因为它们的名称与城市一样,这应该是用户输入,您只需要将路径与用户输入和文件扩展名组合在一起。

def return_path(city):
    fname = city.lower() + '.csv'
    path = os.path.join(path, fname)
    return path

如果您有路径列表,可以使用for循环搜索它们。如果您想在文件夹中搜索给定名称,或者如果您想对文件内容进行任何操作,请指明您的问题。