[PYTHON] Note ((1) csv.reader AttributeError, (2) get_sheet_by_name DeprecationWarning, (3) .delete_cols does not work)

・ Today's progress As a confirmation of the basic function, all the processing shown in the figure below can be performed. As usual, there are many small errors. image.png Make a note of the mistakes you make (1) Uppercase and lowercase letters are tight.

input


import csv
csvfile = open('/content/drive/My Drive/Colab Notebooks/testB.csv',encoding="shift-jis")
reader = csv.Reader(csvfile)
for row in reader:
print(row) 

output


AttributeError                            Traceback (most recent call last)
<ipython-input-14-bb1b8be4844f> in <module>()
      3 csvfile = open('/content/drive/My Drive/Colab Notebooks/testB.csv',encoding="shift-jis") 
----> 4 reader = csv.Reader(csvfile)
      5 for row in reader:
AttributeError: module 'csv' has no attribute 'Reader'

** csv.reader () ** and ** csv.Reader () **, Attribute Error occurred because the case was wrong. Remember that it seems to occur frequently when rewriting from csv.DictReader

② It is better not to use get_sheet_by_name

input


import openpyxl
wb=openpyxl.load_workbook('/content/drive/My Drive/Colab Notebooks/testB.xlsx')
wb.get_sheet_names()

output


/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:3: DeprecationWarning: 
 Call to deprecated function get_sheet_names (Use wb.sheetnames).
  This is separate from the ipykernel package so we can avoid doing imports until
['Sheet1']

Deprecation means deprecated. It works, but it's not so good, so it seems that you should use it, and recommended functions are also written. It's easy to understand if you ask me, but when I saw the character string that popped out, I wasted time asking "What? Why?". You have to read English properly, really ...

③ The hogehoge part of the hogehoge.delete_cols command is the variable name.

Input (before correction)


import openpyxl
wb=openpyxl.load_workbook('/content/drive/My Drive/Colab Notebooks/testB.xlsx')
ws = wb.worksheets[0]
sheet.delete_cols(2)
wb.save('/content/drive/My Drive/Colab Notebooks/testC.xlsx')

The file was output, but the second column of Excel could not be deleted. When I look at it now, I'm completely crazy by eating various sites.

Input (after correction)


import openpyxl
wb=openpyxl.load_workbook('/content/drive/My Drive/Colab Notebooks/testB.xlsx')
ws = wb.worksheets[0]
ws.delete_cols(2)
wb.save('/content/drive/My Drive/Colab Notebooks/testC.xlsx')

Use wb = openpyxl.load_workbook () to pack xlsx data into variable wb With ws = wb.worksheets [0], fill the variable ws with the worksheet data of the argument (0) page from the xlsx data in wb. Delete the second column (argument) in the worksheet data ws with ws.delete.cols (2) Saved with wb.save () and succeeded ... that?

The final output testC.xlsx is the data in which the second column has disappeared properly. Why? Since the ws data is copied from wb, I feel that it is really useless if I do not return the ws data to wb before doing wb.save. When ws = wb.worksheets [] is used, is the ws and wb data synchronized? If you don't know what to look for, why don't you write a question ...?

~~ Postscript ~~ I pasted the link that gave me the answer that the sequence is referenced. https://qiita.com/wellwell3176/questions/d42efcba6482528e1556

Recommended Posts

Note ((1) csv.reader AttributeError, (2) get_sheet_by_name DeprecationWarning, (3) .delete_cols does not work)
LocateCenterOnScreen does not work on PyAutoGui
[VScode] autopep8 format does not work [Python]
Virtualenv does not work on Python3.5 (Windows)
gqlgen command does not work with go gqlgen
After installing php7.2, the php command does not work
Jinja2 2.9.6 does not work on Lambda Python 3 series
When wildcard specification does not work with pylint