python-3.x - CSV 文件:在 Python 中打开并仅打印链接
问题描述
我有 CSV 文件,其中一些链接存储在其中一列中。我只想阅读链接并将它们打印出来。我尝试使用以下代码,但没有输出。
import csv
filename ='abc.csv'
with open(filename,'rb') as f:
reader = csv.reader(f)
for row in reader:
for item in row:
if item.startswith('http'):
print(item)
解决方案
import csv
with open ('abc.csv','r') as csv_file:
csv_reader = csv.reader(csv_file)
for line in csv_reader:
if line[0].startswith('http'):
print(line)
如果您想确保该行以例如“http”开头,您应该编写:
line[0].startswith("http") 因为行列表的第一个元素将是一个字符串。
推荐阅读
- android - Activity 生命周期存储和恢复
- r - 在R中将字符变量更改为日期变量
- python - 如何将下拉小部件输出作为数据框?
- python - 如何将使用 SSH 从 Python 脚本运行的命令的输出存储到文件中?
- wordpress - 在 wwwroot 文件夹下安装 wordpress 容器
- android - 如何在具有所有手势的 SceneView 中使用 TransformableNode?
- excel - 如何使用下拉菜单自动填充其他工作表中的单元格范围
- python - 当我在函数/方法中有输入时,我该如何测试它?
- java - Pentaho 不能只用一个用户登录。所有其他用户工作正常
- ms-access - 如何使用具有多个值的一个参数进行查询?