편집 기록

편집 기록
  • 프로필 nowp님의 편집
    날짜2020.06.18

    list를 for구문에 넣는법에 대해 지식을 나눠주시면 감사하겠습니다


    #영업이익
        year_list = []
        for filename in name_list:
            book = openpyxl.load_workbook(filename)
            sheet = book.worksheets[0]
            def findExistString(searchString, text):
                if searchString in text: return True
                else: return False
            def findNonExistString(searchString, text):
                if searchString in text: return False
                else: return True
            for row in sheet.rows:
                if findExistString("영업", row[1].value):
                        year_list.append(filename[7:14])
                        break
    

    이렇게 해서 year_list를 추출했는데요 out값이

        ['2011.12',
         '2012.12',
         '2013.12',
         '2014.12',
         '2015.12',
         '2016.12',
         '2017.12',
         '2018.12',
         '2019.12']
    

    이렇게 나왔습니다. 이대로 for 구문에 넣으니 실행이 안되더군여. 아래는 for-loop구문입니다

        sheet1 = wb.active
        file_name = '한전.xlsx'
        sheet1.title = 'sampleSheet'
        for coulumn_index in year_list:
            sheet1.cell(row=1, column=coulumn_index).value = coulumn_index
    
        wb.save(filename=file_name)
    

    out


    TypeError                                 Traceback (most recent call last)
    <ipython-input-68-380cc469c2fa> in <module>
          3 sheet1.title = 'sampleSheet'
          4 for coulumn_index in year_list:
    ----> 5     sheet1.cell(row=1, column=coulumn_index).value = coulumn_index
          6 
          7 wb.save(filename=file_name)
    
    ~\anaconda3\lib\site-packages\openpyxl\worksheet\worksheet.py in cell(self, row, column, value)
        233         """
        234 
    --> 235         if row < 1 or column < 1:
        236             raise ValueError("Row or column values must be at least 1")
        237 
    
    TypeError: '<' not supported between instances of 'str' and 'int'
    

    str(year_list)로도 실행해 보았지만 안되더군여 제가 만들고자하는건 year_list를 뽑고 그데이터를 엑셀 에 나열시키는게 목표입니다!

  • 프로필 알 수 없는 사용자님의 편집
    날짜2020.06.18

    list를 for구문에 넣는법에 대해 지식을 나눠주시면 감사하겠습니다


    영업이익

    year_list = []
    for filename in name_list:
        book = openpyxl.load_workbook(filename)
        sheet = book.worksheets[0]
        def findExistString(searchString, text):
            if searchString in text: return True
            else: return False
        def findNonExistString(searchString, text):
            if searchString in text: return False
            else: return True
        for row in sheet.rows:
            if findExistString("영업", row[1].value):
                    year_list.append(filename[7:14])
                    break
    이렇게 해서  year_list를 추출했는데요 out값이
    ['2011.12',
     '2012.12',
     '2013.12',
     '2014.12',
     '2015.12',
     '2016.12',
     '2017.12',
     '2018.12',
     '2019.12']
    이렇게 나왔습니다. 이대로 for 구문에 넣으니 실행이 안되더군여 ㅠㅠ
    

    아래는 forloop구문입니다

    sheet1 = wb.active
    file_name = '한전.xlsx'
    sheet1.title = 'sampleSheet'
    for coulumn_index in year_list:
        sheet1.cell(row=1, column=coulumn_index).value = coulumn_index
    
    wb.save(filename=file_name)
    

    out


    TypeError Traceback (most recent call last) in 3 sheet1.title = 'sampleSheet' 4 for coulumn_index in year_list: ----> 5 sheet1.cell(row=1, column=coulumn_index).value = coulumn_index 6 7 wb.save(filename=file_name)

    ~\anaconda3\lib\site-packages\openpyxl\worksheet\worksheet.py in cell(self, row, column, value) 233 """ 234 --> 235 if row < 1 or column < 1: 236 raise ValueError("Row or column values must be at least 1") 237

    TypeError: '<' not supported between instances of 'str' and 'int'

    ​ str(year_list)로도 실행해 보았지만 안되더군여 제가 만들고자하는건 year_list를 뽑고 그데이터를 엑셀 에 나열시키는게 목표입니다!