0

I have a list of urls in a csv file and I need to run them in for eachUrl in final_url: but I keep getting a traceback error because I find out that when I print the urls from the below code, it is also printing out ['https://www.anyurl.com'] and not just the url. I have tried to run a loop to replace the [' '] but I get the traceback error AttributeError: 'list' object has no attribute 'replace'. How do I open the csv and just pull out the URL's and put them into a list without putting them into a list inside another list?

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/']
4
  • 2
    A CSV file contains rows and each row contains cells. So yes, each row is a list of cells. Now, since your row is just ['https://www.anyurl.com'], i.e. it has only one cell, I would say that this is not really a CSV file at all. Why are you reading it as if it was CSV? Commented Aug 30, 2018 at 19:23
  • This is not a MCVE. Where is your final_urls? And it would be also good if you can provide us an excerpt from your csv file. Commented Aug 30, 2018 at 19:26
  • 2
    @kamikaze can u post contents of urls_for_BrightScope_Form5500s or some sample data. Commented Aug 30, 2018 at 19:27
  • @Tanmayjain I added output above. Commented Aug 30, 2018 at 19:56

2 Answers 2

2

Assuming that the input file (urls_for_BrightScope_Form5500s.csv) is just a plain text file with one URL in each line, I'd say you should just read the lines, without any csv library:

with open('urls_for_BrightScope_Form5500s.csv', 'rt') as f:
    final_urls = [line.strip() for line in f]
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming that you have only one url in a line, you can also just use line reading, e.g.:

with open('urls_for_BrightScope_Form5500s.csv', 'r') as fr:
    final_urls = [url.strip() for url in fr]

1 Comment

lol almost word for word exactly the same answer as mine :D great minds think alike ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.