用户输入从.csv文件生成新列表?
I have a question here that I was hoping some kind soul would help answer!
我在这里有一个问题,我希望有一些善良的人帮助回答!
I have a .csv file with three items in the header, and many assigned. The .csv looks like this when printed:
我有一个.csv文件,标题中有三个项目,许多已分配。打印时.csv看起来像这样:
['NUTS CODE', 'NUTS NAME', 'Change 2008-2014']
['AT11', 'Burgenland (AT)', '0.6']
['AT12', 'Niederosterreich', '1.4']
['AT13', 'Wien', '2.9']
['AT21', 'Karnten', '2.4']
['AT22', 'Steiermark', '1.1']
['AT31', 'Oberosterreich', '1.3']
['AT32', 'Salzburg', '0.7']
['AT33', 'Tirol', '0.6']
['AT34', 'Vorarlberg', '-0.7']
['BE10', 'Region de Bruxelles-Capitale / Brussels Hoofdstedelijk Gewest', '2.4']
['BE21', 'Prov. Antwerpen', '1.5']
['BE22', 'Prov. Limburg (BE)', '1.2']
['BE23', 'Prov. Oost-Vlaanderen', '0.7']
['BE24', 'Prov. Vlaams-Brabant', '0.8']
['BE25', 'Prov. West-Vlaanderen', '1.5']
I want to create a program that takes a user input where the input relates to the first value, say I input : "AT", and the program then returns all the corresponding names and values pertaining to that code. example: If I input "BE" the code creates the following list : [["BE10", "Region de Bruxelles-Capitale / Brussels Hoofdstedelijk Gewest", 2.4], [BE21', 'Prov. Antwerpen', '1.5']]
etc.
我想创建一个程序,它接受用户输入,其中输入与第一个值相关,比如我输入:“AT”,然后程序返回与该代码有关的所有相应名称和值。示例:如果我输入“BE”,代码将创建以下列表:[[“BE10”,“Region de Bruxelles-Capitale / Brussels Hoofdstedelijk Gewest”,2.4],[BE21','Prov。安特卫普','1.5']]等
This is what I use to open:
这是我用来打开的:
import csv
with open ('Change_in_Unemployment_2008-2014.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
#next(readCSV) #Uncomment to remove header
for row in readCSV:
print(row)
csvfile.close()
2 个解决方案
#1
0
result = []
user_input = input()
for row in readCSV:
if row[0].startswith(user_input):
result.append(row)
print (result)
更多相关文章
- 求助:Python是否可以用一行代码来同时给变量赋值并打印变量的值
- Pycharm 运行跳一跳程序
- [Python]—Linux Server 系统监控程序
- 长安铃木经销商爬取(解析xml、post提交、python中使用js代码)
- 在两台服务器上有效地在两个Django应用程序之间进行通信(多租户)
- 高级程序员装逼指南
- 购物车程序练习
- 如何停止Py_Initialise应用程序的崩溃?
- Python语言的特点、程序设计基本方法