gpt4 book ai didi

Python 无法在 csv 文件中找到字符串

转载 作者:行者123 更新时间:2023-11-30 22:32:00 24 4
gpt4 key购买 nike

我试图学习如何使用 python 在 csv 中查找字符串或值。我认为这很简单,但是当我尝试执行我的代码时,我不明白为什么我的代码找不到我想要的字符串。这是我的代码:

import csv

with open('sample2.csv', 'rt') as baca:
read = csv.reader(baca, delimiter='\'')
for row in read:
match = row[0]
if match == "Sample":
print match

我还尝试打印“row[0]”,结果如下:

id      name         number
1 Sample 0123450000007

打印没有索引的“行”:

{'id\tname\tnumber': '1\tSample\t0123450000007'}

截图:

enter image description here

最佳答案

看起来 demlter 应该是一个选项卡

In [9]: import csv
...:
...: with open('sample2.csv', 'rt') as baca:
...: read = csv.reader(baca, delimiter='\t')
...: for row in read:
...: match = row[1]
...: if match == "Sample":
...: print match
Sample

关于Python 无法在 csv 文件中找到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45626238/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com