admin管理员组文章数量:1404589
My input file, infile is as follows:
NUMBER,SYMBOL
1,AAPL
2,MSFT
3,NVDA
Here is my code:
import csv
infile = "stock-symbols-nasdaq-SO.csv"
with open(infile, encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile) # Read CSV as a dictionary
for row in reader:
print(row)
symbol = row['SYMBOL'] # Error from Pycharm: Expected type 'SupportsIndex | slice', got 'str' instead
print(symbol)
This code "appears" to run correctly, but why is Pycharm indicating an error? Is their anything that I can do to remove it?
My input file, infile is as follows:
NUMBER,SYMBOL
1,AAPL
2,MSFT
3,NVDA
Here is my code:
import csv
infile = "stock-symbols-nasdaq-SO.csv"
with open(infile, encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile) # Read CSV as a dictionary
for row in reader:
print(row)
symbol = row['SYMBOL'] # Error from Pycharm: Expected type 'SupportsIndex | slice', got 'str' instead
print(symbol)
This code "appears" to run correctly, but why is Pycharm indicating an error? Is their anything that I can do to remove it?
Share Improve this question edited Mar 10 at 16:58 Daraan 4,2427 gold badges22 silver badges47 bronze badges asked Mar 10 at 16:44 Charles KnellCharles Knell 6434 silver badges16 bronze badges 5 |1 Answer
Reset to default 0A solution to this is to use a type hint for row as follows:
with open(infile, encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile) # Read CSV as a dictionary
for row in reader:
print(row)
row: dict[str, str] # type hint which specifies that row is a dict with string keys and values
symbol = row['SYMBOL']
print(symbol)
本文标签:
版权声明:本文标题:python - Error from Pycharm: Expected type 'SupportsIndex | slice', got 'str' instead - Stack Ov 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744834344a2627541.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
row
should be a dict, the error indicates that row is inferred as alist
. – Daraan Commented Mar 10 at 16:56