filename = selectedCompany + "/" + "1" + ".txt" # define filename to save file with
for row in csv_file:
if selectedCompany == row[1]:
count +=1
if not os.path.exists(os.path.dirname(filename)):
try:
os.makedirs(os.path.dirname(filename))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
totalCnt=0
print listOfCompanies #to debug
print selectedCompany #to debug
while totalCnt < len(csv_file): # as long as total count is lesser than length of csv file
totalCnt+=1 # add total count of 1
for row in csv_file:
if selectedCompany == row[:][1]: # if selected company equals to the company in csv file
with open(filename, "w") as output: # save the .txt file
output.write(str(csv_file[totalCnt])) # save the contents of the csv file
else:
totalCnt+=1 # add total count of 1
모두 안녕하세요. 저는 csv 파일에서 데이터를 읽고 개별 .txt 파일로 내보내는 데 Python을 사용하고 있습니다. 내 질문은 특정 행의 특정 필드를 어떻게 참조합니까? 다음 CSV 파일에 설정된CSV 파일에서 쿼리하는 동안 행의 특정 필드를 참조하는 방법
예내 데이터는
이름 성별 연령
존 m 15
메리 (이름, 사용자의 입력에
샘 m 12
13 F ie sam), 데이터의 전체 행 (예 : "sam, m, 12")을 데이터로 내보내고 .txt 파일로 내보낼 수 있기를 바랍니다.
예를 들어 샘의 성별을 지적하고 싶다면 어떻게해야합니까? 그리고 for 루프를 올바르게 사용하여 원하는 모든 .txt 파일을 내보낼 수 있는지 확인하려면 어떻게합니까?
내 경우에는 논리 오류가 발생하지만 문제를 해결하기는 정말 힘듭니다.
정말 도움이됩니다.
편집 :
오류 메시지 팬더 모듈을 사용 후 : 내가 당신이라면
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 1797, in
__getitem__
return self._getitem_column(key)
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 1804, in _
getitem_column
return self._get_item_cache(key)
File "C:\Python27\lib\site-packages\pandas\core\generic.py", line 1084, in
_get_item_cache
values = self._data.get(item)
File "C:\Python27\lib\site-packages\pandas\core\internals.py", line 2851,
in get
loc = self.items.get_loc(item)
File "C:\Python27\lib\site-packages\pandas\core\index.py", line 1572, in
get_loc
return self._engine.get_loc(_values_from_object(key))
File "pandas\index.pyx", line 134, in pandas.index.IndexEngine.get_loc
(pandas\index.c:3824)
File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc
(pandas\index.c:3704)
File "pandas\hashtable.pyx", line 686, in p
pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12280)
File "pandas\hashtable.pyx", line 694, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12231)
KeyError: False
표준 라이브러리의 [csv] (https://docs.python.org/3/library/csv.html) 모듈을 살펴 보겠습니다. 그것은 당신이 subscript 할 수있는리스트 (col by index)로 구성된 편리한'reader()'객체를 제공합니다. – pstatix
또는 심지어 사전 헤더로 col 헤더를 사용하는'DictReader' – Adirio
안녕하세요, 내 대답을 확인하고 더 도움이 필요하십니까 도움을 요청하십시오. –