代码之家  ›  专栏  ›  技术社区  ›  Kamikaze_goldfish

Python CSV reader在列表中创建列表

  •  0
  • Kamikaze_goldfish  · 技术社区  · 7 年前

    for eachUrl in final_url: ['https://www.anyurl.com'] 不仅仅是网址。我试着运行一个循环来替换 [' '] 但我得到了回溯错误 AttributeError: 'list' object has no attribute 'replace'

    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/']
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   colidyre    7 年前

    假设一行中只有一个url,也可以使用行读取,例如:

    with open('urls_for_BrightScope_Form5500s.csv', 'r') as fr:
        final_urls = [url.strip() for url in fr]
    
        2
  •  2
  •   zvone    7 年前

    假设输入文件( urls_for_BrightScope_Form5500s.csv csv 图书馆:

    with open('urls_for_BrightScope_Form5500s.csv', 'rt') as f:
        final_urls = [line.strip() for line in f]