이 소스 코드의 목적은 국가 채우기와 국가 이름 만 인쇄하는 것입니다. 필자는 Python을 처음 사용하고 CSV 및 데이터베이스 작업을 새로하고 있지만 CSV에 데이터를 저장하면 in a code review이 더 잘 작동한다고 들었습니다.Python의 CSV 파일에서 열 숨기기
해결 : 또 다른 문제가 있습니다. 국가를 물색 할 때, 당신이 옳거나 틀린 나라에 들어 갔는지 여부는 문제가되지 않으며, 여전히 목록에없는 것으로 간주됩니다.
소스 코드 :
import random
import csv
print '\nCountries to choose from are\n'
country_list = []
with open('countries.csv', 'rb') as csvfile:
countryreader = csv.reader(csvfile, delimiter=',', quotechar='|')
for idx, country in enumerate(countryreader):
print ' '.join(country) # everything besides population and country title should be hidden
if (idx != 0): # Don't append the fist row (column headers)
country_list.append(country[0])
startcountry = raw_input('\nWhat country would you like to start in? If you can\'t think of one, you can enter random to choose a random country.').title()
if startcountry == 'Random': # random.sample can be used for more than one country later on
startcountry = random.choice(country_list)
while startcountry not in country_list:
startcountry = raw_input('We don\'t know that country. Please enter one from the list above. ')
CSV 파일 : 당신이 구분 기호로 공간을 사용하고 있지만, CSV 파일이 구분 기호로 쉼표를 사용하고 있기 때문에
Country,Population,Type
China,1363560000,Urban
India,1242070000,Rural
United States,317768000,Urban
Indonesia,249866000,Rural
Brazil,201032714,Rural
Pakistan,186015000,Unspecified
Nigeria,173615000,Unspecified
Bangladesh,152518015,Unspecified
Russia,143700000,Rural
Japan,127120000,Urban
Mexico,119713203,Urban
Philippines,99329000,Unspecified
Vietnam,89708900,Unspecified
Egypt,86188600,Unspecified
Germany,80716000,Urban
Iran,77315000,Unspecified
Turkey,76667864,Unspecified
Thailand,65926261,Unspecified
France,65844000,Urban
United Kingdom,63705000,Urban
Italy,59996777,Urban
South Africa,52981991,Unspecified
South Korea,50219669,Unspecified
Colombia,47522000,Rural
Spain,46609700,Unspecified
Ukraine,45410071,Rural
Kenya,44354000,Unspecified
Argentina,40117096,Rural
Poland,38502396,Rural
Sudan,37964000,Rural
Uganda,35357000,Unspecified
Canada,35344962,Unspecified
Iraq,34035000,Unspecified
나는 파이썬을 배우는 중이다. "프로덕션"프로젝트의 경우 CSV 구문 분석 및 데이터 읽기/조작/평가에 팬더를 사용하는 것이 좋습니다. 당신은 진지하게 그것에 들여다 볼 수 있습니다. 이 질문이 학습에 관한 것이면 그 권고를 무시할 수 있습니다. –
첫 질문에 '조국'에 '조인'이 들어가기 전에 실제로 무엇을 보지 않으시겠습니까? 두 번째로, 더 많은 정보를 제공하십시오 - 대신 어떤 일이 일어나고 나머지 코드는 어디에 있습니까? 'country_list'에 무엇이 있습니까? 무작위 옵션이 효과가 있습니까? – jonrsharpe