List not allowing .splitlines() - Python

时光总嘲笑我的痴心妄想 提交于 2019-12-25 14:45:38

问题


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

回答1:


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]

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:


来源:https://stackoverflow.com/questions/32897405/list-not-allowing-splitlines-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!