【问题标题】:Python csv file lookupPython csv 文件查找
【发布时间】:2018-03-13 23:10:14
【问题描述】:

在一个项目中,我们获得了三个 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')

【问题讨论】:

  • 你有什么问题?
  • 欢迎来到 SO。不幸的是,这不是一个讨论论坛或教程。请花时间阅读How to Ask 和该页面上的其他链接。花一些时间与the Tutorial 一起练习示例。它会让你了解 Python 提供的工具来帮助你解决问题。

标签: python csv


【解决方案1】:

我假设您有一个存储所有文件的特定文件夹或文件夹列表。因为它们被命名为城市,而这应该是用户输入,您只需将路径与用户输入和文件扩展名结合起来。

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

如果您有一个路径列表,您可以使用 for 循环搜索它们。如果您想在文件夹中搜索一个给定的名称,或者您想对文件的内容做任何事情,请说明您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 2021-10-11
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    • 2014-12-20
    相关资源
    最近更新 更多