我在一个csv文件中有一个URL列表,我需要在for eachUrl in final_url:
中运行它们,但是我不断收到回溯错误,因为我发现当我从下面的代码打印URL时,它也是打印出['https://www.anyurl.com']
而不仅仅是网址。我尝试运行一个循环来替换[' ']
,但收到回溯错误AttributeError: 'list' object has no attribute 'replace'
。我如何打开csv并仅拉出URL并将其放入列表中,而不将其放入另一个列表中的列表中?
import csv
with open('urls_for_BrightScope_Form5500s.csv', 'r') as r:
reader = csv.reader(r)
for row in reader:
final_urls.append(row)
print(len(final_urls))
Output from final_url list: ['https://www.brightscope.com/401k-rating/372254/Merritt-Brothers-Lumber-Company/377291/Merritt-Brothers-Lumber-Co-401K-Profit-Sharing-Plan/'], ['https://www.brightscope.com/401k-rating/255132/Merritt-Club-Management-Inc/259235/Merritt-Club-Management-Inc-401K-Profit-Sharing-Plan-And-Trust/'], ['https://www.brightscope.com/401k-rating/404751/Merritt-Equipment-Co/410055/Merritt-Equipment-Co-401K-Profit-Sharing-Plan/'], ['https://www.brightscope.com/401k-rating/256405/Merritt-Hospitality-Llc/260527/Merritt-Hospitality-401K-Plan/']
答案 0 :(得分:2)
假设输入文件(urls_for_BrightScope_Form5500s.csv
)只是纯文本文件,每行只有一个URL,我想您应该阅读这些行,而无需任何csv
库:>
with open('urls_for_BrightScope_Form5500s.csv', 'rt') as f:
final_urls = [line.strip() for line in f]
答案 1 :(得分:1)
假设一行中只有一个网址,您也可以只使用行读取,例如:
with open('urls_for_BrightScope_Form5500s.csv', 'r') as fr:
final_urls = [url.strip() for url in fr]