本文介绍了列表不允许.splitlines()-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要做些什么来防止错误:AttributeError: 'list' object has no attribute 'split lines'
在这里发生?如何将我拥有的列表转换为可以归因于splitlines
的形式?
What do I need to do to prevent the error: AttributeError: 'list' object has no attribute 'split lines'
from occurring here? How to I convert the list that I have into a form that can have splitlines
attributed to?
import requests
import re
from bs4 import BeautifulSoup
import csv
#Read csv
with open ("gyms4.csv") as file:
reader = csv.reader(file)
csvfilelist = [row[0] for row in reader]
print csvfilelist
#Get data from each url
def get_page_data():
for page_data in csvfilelist.splitlines():
r = requests.get(page_data.strip())
soup = BeautifulSoup(r.text, 'html.parser')
yield soup
推荐答案
str.splitlines()
方法仅适用于字符串对象.您没有字符串对象,但有一个字符串列表:
The str.splitlines()
method only works on a string object. You don't have a string object, you have a list of strings:
csvfilelist = [row[0] for row in reader]
无需拆分此文件,因为文件中已经有每一行的第一列.只需删除.splitlines()
调用:
There is no need to split this, you already have the first column of each line in the file. Just remove the .splitlines()
call:
for page_data in csvfilelist:
这篇关于列表不允许.splitlines()-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!