Good day. Excel 스프레드 시트에는 다음과 같은 구조의 데이터가 있습니다.
다음과 같은 형식으로 3 개의 열에 고유 한 값을 함께 넣고 싶었습니다.
Mike to America for Hotel; Meal
과
Kate to Europe; America for Transport
기타
나는 2 개의 열에 대해서만 운동 할 수 있습니다.
the_file = xlrd.open_workbook("testing.xlsx")
the_sheet = the_file.sheet_by_name("Sheet1")
products = defaultdict(list)
for row_index in range(1, the_sheet.nrows):
products[str(the_sheet.cell(row_index, 0).value)].append(the_sheet.cell(row_index, 1).value)
for product, v in products.items()
print product + " to " + ";".join(set(v))
출력은 다음과 같습니다.
Mike to America
Hulk to America;Asia
Kate to Europe;America
Dave to Europe
Jack to Europe;America;Asia
Luci to Asia
3 개의 열에 대해 선이 함께 작동하도록하는 방법은 무엇입니까?
감사합니다.
그것을 수행하는 더 비단뱀적인 방법이 있다고 생각하지만 이것이 내가 생각해 낸 것입니다.
from collections import defaultdict
l = [
['mike', 'america', 'hotel'],
['mike', 'america', 'meal'],
['jack', 'america', 'meal'],
['jack', 'europe', 'hotel'],
['jack', 'america', 'bonus'],
['jack', 'asia', 'hotel'],
['dave', 'europe', 'meal'],
]
people = defaultdict(list)
people_places = defaultdict(list)
for row_index in range(len(l)):
people[l[row_index][0]].append(l[row_index][1])
people_places[l[row_index][0] + '|' + l[row_index][1]].append(l[row_index][2])
for p, k in people.items():
activity = []
for place in k:
activity += people_places[p + '|' + place]
print '{} to {} for {}'.format(
p,
';'.join(set(k)),
';'.join(set(activity))
)
코드를 번역하여 스프레드 시트 행과 셀을 직접 사용하거나 l
다음과 같이 먼저 목록을 추출 할 수 있습니다.
l = []
with xlrd.open_workbook("testing.xlsx") as the_file:
the_sheet = the_file.sheet_by_name("Sheet1")
for row_index in range(1, the_sheet.nrows):
l.append([
the_sheet.cell(row_index, 0).value,
the_sheet.cell(row_index, 1).value,
the_sheet.cell(row_index, 2).value])
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다