简体   繁体   English

python中csv文件中循环的输出

[英]Output of a loop in csv file in python

In this script, I have to dissect some URLs and print the output into a CSV file.在这个脚本中,我必须剖析一些 URL 并将输出打印到 CSV 文件中。

import requests
import CSV
from urllib.parse import urlparse

with open(r'C:\Users\acer\Desktop\Project\WordPress\output.csv', 'w', newline='') as file: ==>doubt
    writer = csv.writer(file)                                                              ==>doubt
def content_length(domain):
    try:
        r = requests.get(domain.strip())
        surl = r.url
        urlp = urlparse(surl)
        furl = urlp.scheme + '://' + urlp.hostname + '/'
        rd = requests.get(furl.strip())
        rdstat = rd.status_code
        #print(rdstat)
        if rd.status_code == 403:
            rdmsg = '403 - Forbidden'
        elif rd.status_code == 200:
            r1 = requests.get(furl, stream = True)
            c_length = r1.headers['content-length']
            rdmsg = c_length
        else:
            rdmsg = 'Not Available'
            #print (rdmsg)
        final_print = str(rdstat) + (',') + rdmsg
        print(final_print)
        writer.writerow(["Source Url", "Final Url", "Status Code", "Content-Length"])  ==>doubt
        writer.writerow([surl,furl,str(rdstat),rdmsg])                                 ==>doubt

    except Exception as e:
        print(domain.strip() + ',' + 'Exception')
        print(e)


print('Response Status Code,Content Length')





with open(r'C:\Users\acer\Desktop\Project\WordPress\domains.txt', 'r') as domlist:
    for dom in domlist:
        content_length(dom)

Here, my problem is that I have to add the output in the CSV file for every iteration(ie every url in the text file).在这里,我的问题是我必须为每次迭代在 CSV 文件中添加输出(即文本文件中的每个 url)。 but, I don't know where to add the code snippet for CSV...但是,我不知道在哪里添加 CSV 的代码片段...

You can try to restructure the code so instead of making one request and then writing one row;您可以尝试重构代码,而不是先提出一个请求,然后再写一行; try to make all the requests, put the data into lists and then write those lists to the csv file.尝试发出所有请求,将数据放入列表中,然后将这些列表写入 csv 文件。

You can do this by returning the data rather than writing it in your content_length function:您可以通过返回数据而不是将其写入content_length函数来实现此目的:

def content_length(domain):
    try:
        r = requests.get(domain.strip())
        surl = r.url
        urlp = urlparse(surl)
        furl = urlp.scheme + '://' + urlp.hostname + '/'
        rd = requests.get(furl.strip())
        rdstat = rd.status_code
        #print(rdstat)
        if rd.status_code == 403:
            rdmsg = '403 - Forbidden'
        elif rd.status_code == 200:
            r1 = requests.get(furl, stream = True)
            c_length = r1.headers['content-length']
            rdmsg = c_length
        else:
            rdmsg = 'Not Available'
            #print (rdmsg)
        final_print = str(rdstat) + (',') + rdmsg
        print(final_print)
        return [surl,furl,str(rdstat),rdmsg]

    except Exception as e:
        print(domain.strip() + ',' + 'Exception')
        print(e)

That will return a list which will be the row you want to write.这将返回一个列表,该列表将是您要写入的行。 Now you'll need another function to get all 'rows' from the domain file:现在您需要另一个函数来从域文件中获取所有“行”:

def all_domains():
    dom_data = []
    with open(r'C:\Users\acer\Desktop\Project\WordPress\domains.txt', 'r') as file:
        domlist = file.readlines()
        for dom in domlist:
            cl = content_length(dom)
            dom_data.append(cl)
    return dom_data

You can now write the result of all_domains to csv using a write_rows :您现在可以使用write_rowsall_domains的结果写入 csv :

domains = all_domains()
with open(r'C:\Users\acer\Desktop\Project\WordPress\output.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    # Write headers
    writer.writerow(["Source Url", "Final Url", "Status Code", "Content-Length"])
    # Write domains
    writer.writerows(domains)

Also when you import the csv package at the top of the file make sure that this is lowercase and not uppercase 'CSV' as this will throw an exemption此外,当您在文件顶部导入csv包时,请确保这是小写而不是大写的“CSV”,因为这将引发豁免

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM