python-3.x - How to use a user input variable to load a csv file in Python
问题描述
Im trying my hand at writing my first Python tool for a part of my job to automate a process (a raffle/random drawing). I want it to ask the user 2 questions: "What month are you drawing for?" and "What year are you drawing for?"
I then want it to take the user input and load/open a csv file in the directory in python using their input.. all the csv files are titled like so..
March2019.csv
April2019.csv
So in my mind I believe that if I store those user inputs as variable I can use them to call the proper csv file
I have already tried the code shown below, but the problem is, you cant put the variables inside the '' when using the pandas.read('') function because it reads them as a string instead of as what the variables represent
month = input("What month are you drawing for? ")
year = input("What year are you drawing for? ")
import pandas
df = pandas.read_csv('month year'.csv)
this doesnt work because its literally looking for a file called 'month year'.csv
解决方案
尝试:
df = pandas.read_csv(month+str(year)+'.csv')
推荐阅读
- python - 可执行路径需要在 Python 中的 PATH 中
- reactjs - 我的 React Bootstrap 手风琴的第一张卡片打不开
- amazon-web-services - Amazon Simple Email Service 将于 2020 年 9 月 30 日终止对 Signature Version 3 的支持
- vue.js - 使用 Vue 测试工具和 Jest 测试 Vuetify 表单验证
- javascript - 如何在 JavaScript 中使用单个 for 循环访问二维数组?
- angular - Angular 组件之间的数据流问题
- c++ - 我可以使用 Podofo 或 libHaru 从 PDF 图像文件中获取字节数组吗?
- javascript - 防止 Elementor Multi-Step Form textarea 自动更改为下一步
- flutter - Flutter PaginatedDataTable Future 因链接请求而失败
- graphql - 我的 apollo graphql 订阅用例是否有保证?