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)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744834344a4596173.html
row
should be a dict, the error indicates that row is inferred as alist
. – Daraan Commented Mar 10 at 16:56