I am writing some code. I would like to have an if
statement that if a row in the CSV file contains what you input (a single word) then it does something.
I have tried doing if row = input then do this
but it doesn't work as the whole row does not = the input only part of it does.
for row in reader:
if row == input1:
# Whatever I want
It does nothing, as the entire row does not equal input1
.
您可以使用以下if input1 in row:
You can use
if row[0]==input1:
This will check if the first element in the row is equal to input1.
Maybe something like the following.
with open('country.csv', 'r', encoding="utf-8") as myfile:
data=myfile.readlines()
for line in data:
if "Switzerland" in line:
print(line)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.