繁体   English   中英

python 如何使用 csv 文件中的数据运行函数

[英]python How to run a function using data from csv file

我有一个在 pinterest 上执行任务的代码,它是手动工作的,必须在“pin_id=”区域填写一个数字,当我运行时,将完成重新固定,但是,我想:

  1. 从一列 csv 文件中填充“pin_id”区域中的数据。
  2. 每个任务之间有一些延迟(5-10 秒)。
  3. 能够限制每次运行运行的行数,例如 50,CSV 文件可能有数百个数据,我不想一次运行所有数据。

[edited] 代码已编辑,我试过的最新的,代码如下:

 import json
 import random
 import time
 import os
 from csv import reader
 import csv
 import pandas as pd
 from py3pin.Pinterest import Pinterest

 pinterest = Pinterest(email='xxxx',
                  password='xxxx',
                  username='xxxx',
                  cred_root='cred_root')
  def repin(pin_id='', board_id=myboard Id, section_id=None):
  return pinterest.repin(board_id=board_id, pin_id=pin_id, 
  section_id=section_id)

  with open('pin-test.csv','r') as csvfile:  # use with to auto-close file
  for row in csvfile.readlines():  # pin_id
   repin(row)  # board_id and section can use defaults
   time.sleep(random.randint(1,3)) # wait 1-3 seconds

到目前为止还没有限制要运行的行数的功能,

无论如何,编辑过的脚本也不起作用,

运行时我得到这些:

C:\Users\Dav111\Desktop\Python\py3-pinterest-master>z-repin-test.py
Traceback (most recent call last):

File "C:\Users\Dav111\Desktop\Python\py3-pinterest-master\z-repin-test.py", 
line 40, in <module>
repin(row)  # board_id and section can use defaults

File "C:\Users\Dav111\Desktop\Python\py3-pinterest-master\z-repin-test.py", 
line 35, in repin
return pinterest.repin(board_id=board_id, pin_id=pin_id, 
section_id=section_id)

File "C:\Users\Dav111\Desktop\Python\py3-pinterest- 
master\py3pin\Pinterest.py", line 433, in repin
return self.post(url=REPIN_RESOURCE_CREATE, data=data)

File "C:\Users\Dav111\Desktop\Python\py3-pinterest- 
master\py3pin\Pinterest.py", 
line 111, in post
return self.request('POST', url=url, data=data, files=files, 
extra_headers=headers)

File "C:\Users\Dav111\Desktop\Python\py3-pinterest- 
master\py3pin\Pinterest.py", line 103, in request
response.raise_for_status()

File "C:\python\Python38\lib\site-packages\requests\models.py", line 941, in 
raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: 
https://www.pinterest.com/resource/RepinResource/create/

 

感谢您对此的帮助。

这看起来很简单。 只需在迭代数据文件时调用该函数。

import random, time

def repin(pin_id='Id number from csv file', board_id=12345, section_id=None):
   return pinterest.repin(board_id=board_id, pin_id=pin_id, section_id=section_id)
   

with open('pin-test.csv','r') as csvfile:  # use with to auto-close file
   for row in csvfile.readlines():  # pin_id
       repin(row)  # board_id and section can use defaults
       time.sleep(random.randint(1,3)) # wait 1-3 seconds

暂无
暂无

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

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