简体   繁体   English

output.csv 文件只写最后一个查询。 如何让它打印所有 url 支票?

[英]output .csv file only writing last query. How do I get it to print all url checks?

input.csv has a list of urls in one column to check. input.csv 有一列中的 url 列表要检查。 It runs these checks perfectly and then the script is supposed to write out which sites are up and which are down.它完美地运行这些检查,然后脚本应该写出哪些站点启动,哪些站点关闭。 However, the last part of the code only seems to write the last site checked.但是,代码的最后一部分似乎只写了最后一个站点检查。 How can I get it to write all checks to the output.csv?如何让它将所有检查写入 output.csv?

import re
import requests
from itertools import product, cycle
import csv
from urllib.request import urlopen


with open("input.csv") as f:
    for row in csv.reader(f):
        s = 'Quantity' or 'Stock'
        r = requests.get(row[0])
        result = re.search(s, r.text)
        if result == None:
            print("OUT OF STOCK")
            inis = "OUT OF STOCK"
        else:
            print("In Stock")
            inis = "In Stock"

        new_column = [(row[0]) + " " + inis]
        print(row[0])


with open("output.csv", "w") as f:
    for col in new_column:
        f.write(col + "\n")

the script is supposed to write out which sites are up and which are down该脚本应该写出哪些站点已启动,哪些站点已关闭

I'm not sure where the 'stock' terminology comes from, but assuming the logic code is correct, why not try something like this...我不确定“库存”术语的来源,但假设逻辑代码是正确的,为什么不尝试这样的事情......

Create an empty list first, then on each iteration of the loop append a row to it:首先创建一个空列表,然后在循环 append 的每次迭代中添加一行:

with open("input.csv") as f:
    new_column = []
    for row in csv.reader(f):
        s = 'Quantity' or 'Stock'
        r = requests.get(row[0])
        result = re.search(s, r.text)
        if result == None:
            inis = "OUT OF STOCK"
        else:
            inis = "In Stock"

        new_column.append(f"{row[0]},{inis}")

        print(row[0], inis)

new_column is now a list of CSV compatible rows, which can be written to output.csv with your exisiting code. new_column现在是 CSV 兼容行的列表,可以使用现有代码将其写入output.csv

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

相关问题 如何仅打印最后一个 output? - How do I print the last output only? 将打印输出写入csv文件 - writing print output to csv file 如何获得仅打印一个输出的代码? - How do I get my code to only print one output? 我如何用熊猫打印所有 csv - how do I print all csv with pandas 如何将我的 python output 放入 csv 或 ZBF57C906FA7D2BB66D07372E41585D9 文件中? - How do I get my python output into a csv or excel file? 编写python脚本以抓取excel数据并写入CSV,如何获得正确的输出? - Writing a python script to scrape excel data and write to a CSV, how do I get the proper output? 为什么我只能在输出文件中得到最后的输出? - Why I only get the last output in my output file? 我想在 csv 文件中写入,它只写入最后一个值(我打印该值并且它有效,但在 csv 中没有) - I want to write in csv file and it only write the last value (I print the value and it work but in csv not) 我如何将所有从 jpg 图像生成的哈希保存到 csv 文件中,而不仅仅是最后一个? - How could i save all the hashes generated from jpg images into a csv file and not only the last one? 如何将所有循环输出放入变量中(用于生成输出文件)? (与CSV相关) - How do I put all my looped output in a variable (for generating an output file)? (CSV related)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM