简体   繁体   English

Python 2.7-通过CSV进行迭代

[英]Python 2.7 - iterate through a CSV

Afternoon, 下午,

I have the following Python 2.7 code below. 我下面有以下Python 2.7代码。 I would like to be able to do the following: 我希望能够执行以下操作:

  1. Read a CSV file which contains values for the 'ofile' variable. 读取一个CSV文件,其中包含“ ofile”变量的值。
  2. Run the below script and repeat for each row in the CSV. 运行以下脚本,然后对CSV中的每一行重复一次。
  3. The csv file contains no headers and has the 'ofile' values - one per row in column A. csv文件不包含标题,并且具有“ ofile”值-列A中的每一行一个。

Instead of me running this script, and then updating the ofile variable manually, i would like to automate this process. 而不是我运行此脚本,然后手动更新ofile变量,我想自动执行此过程。

Thanks in advance. 提前致谢。

import time
import pysftp 
import sys
import os
from datetime import datetime
import calendar
import zipfile
import re

ofile = 'abc_'   
oupload = pysftp.Connection(host="xxxx", username="sxx", password="xNxxxx")

d = datetime.utcnow()
unixtime=calendar.timegm(d.utctimetuple())

import datetime
month = datetime.datetime.now().strftime("%m")

string = ofile+month+".*\.txt$"

possibleFiles = oupload.listdir("/")
for filename in possibleFiles:
        filedate = re.search(string, filename)
        if filedate:
            newfile = filename


timestamp  = oupload.stat(newfile).st_atime  

if timestamp != unixtime: 

        newtime=unixtime + 1800  
        zipname = ofile+str(newtime)+'.sync.zip'

        create_zip = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED) 

        oupload.get(newfile, newfile)
        oupload.close()

        newfilename = ofile+str(newtime)+'.sync' 
        os.rename(newfile, newfilename)


        create_zip.write(newfilename)

        create_zip.close()


else: 

        print "No file found"

The CSV file has no headers, and only has filenames in column A. Here is a snippet: CSV文件没有标题,并且在A列中只有文件名。这是一个代码段:

filenamec
filename_b
filename_erf

You need to convert your script in a function first, then you iterate over each line of the csv file, you extract the value you need and assign it the to ofile variable.Finally, run the function passing ofile as an argument. 首先需要在函数中转换脚本,然后遍历csv文件的每一行,提取所需的值并将其分配给ofile变量。最后,运行将ofile作为参数传递的函数。 I can't be more precise since you haven't posted any example of the actual csv, but the implementation should look something like this: 由于您尚未发布任何实际csv的示例,因此我无法更精确地说明问题,但是实现应如下所示:

import time
import pysftp 
import sys
import os
from datetime import datetime
import calendar
import zipfile
import re


def myscript(ofile):  
    oupload = pysftp.Connection(host="xxxx", username="sxx", password="xNxxxx")
    d = datetime.utcnow()
    unixtime=calendar.timegm(d.utctimetuple())
    month = datetime.now().strftime("%m")
    string = ofile+month+".*\.txt$"

    possibleFiles = oupload.listdir("/")
    for filename in possibleFiles:
            filedate = re.search(string, filename)
            if filedate:
                newfile = filename

    timestamp  = oupload.stat(newfile).st_atime  
    if timestamp != unixtime:
        newtime=unixtime + 1800
        zipname = ofile+str(newtime)+'.sync.zip'
        create_zip = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED)
        oupload.get(newfile, newfile)
        oupload.close()
        newfilename = ofile+str(newtime)+'.sync' 
        os.rename(newfile, newfilename)
        create_zip.write(newfilename)
        create_zip.close()

    else: 
        print "No file found"


mycsv = 'mydocument.csv'

with open(mycsv, 'r') as f:
    for line in f:
        if not line.startswith('\n'): #skip empty lines
            ofile = line.strip() #assuming you have only one column
            myscript(ofile)

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

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