我想读取服务器中的远程TEXT文件,并通过用“”(空格)分割的每一行对其进行处理,并找到其中存在单词“ Stock:”的行,并获取这些行和拆分值然后处理它们,然后将数据插入另一个远程数据库。
已在本地完成并且工作正常,脚本的一部分如下所示。
因此,当我用Google搜索和研究发现时,可以使用paramiko库访问远程文件,但是我不确定如何根据需要使用它。
我已将以下代码粘贴为您的建议,请让我知道我在以下代码中需要做的所有更改。
任何建议都会有很大帮助。
import psycopg2
import time
import os
import MySQLdb
import paramiko
from utils.config import Configuration as Config
from utils.postgres_helper import get_connection
from utils.utils import get_global_config
start_time = time.perf_counter()
#Magento connection
try:
source_host = 'magento'
conf = get_global_config()
host=conf.get(source_host, 'host')
user=conf.get(source_host, 'user')
passwd=conf.get(source_host, 'password')
port=int(conf.get(source_host, 'port'))
db=conf.get(source_host, 'db'))
except mysql.connector.Error as e:
print ("MYSQL: Unable to connect!", e.msg)
sys.exit(1)
# Postgresql connection
try:
cnx_psql = get_connection(get_global_config(), 'pg_dwh')
print ("DWH Connected")
except psycopg2.Error as e:
print('PSQL: Unable to connect!\n{0}').format(e)
sys.exit(1)
# Cursors initializations
cur_psql = cnx_psql.cursor()
try:
#filePath='''/Users/linu/Downloads/log'''
filePath='''/Aio2/log/stockitem/log.txt'''
table='staging.stock_dump'
SQL="""DROP TABLE IF EXISTS """+ table + """;CREATE TABLE IF NOT EXISTS """+ table + """
(created_date TEXT, product_sku TEXT, previous_stock TEXT, current_stock TEXT );"""
cur_psql.execute(SQL)
cnx_psql.commit()
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(host, username=user, password=passwd)
except paramiko.SSHException:
print "Connection Failed"
quit()
try:
sftp_client = ssh_client.open_sftp()
remote_file = sftp_client.open(filePath)
ftp = ssh.open_sftp()
file=ftp.file(filePath, "r", -1)
data=file.read()
fields = string.split(data) # How i can split it by space.
for i in fields:
if string.find(i, 'Stock:')>-1:
print i
#for line in remote_file:
#if 'Stock:' in line:
#fields=line.split(" ")
#date_part1=fields[0]
#date_part2=fields[1][:-1]
#sku=fields[3]
#prev_stock=fields[5]
#current_stock=fields[7]
#if prev_stock.strip()==current_stock.strip():
#continue
#else:
#cur_psql.execute("insert into " + table+"(created_date, product_sku, previous_stock , current_stock)" + " select CAST('" + date_part1+ " "+ date_part2 + "' AS TEXT)" +", CAST('"+sku+"' AS TEXT),CAST('" + prev_stock +"' AS TEXT),CAST('" +current_stock + "' AS TEXT);")
finally:
remote_file.close()
client.close()
cnx_psql.commit()
cur_psql.close()
cnx_psql.close()
print("Data loaded to DWH from text file")
print("Data porting took %s seconds to finish---" % (time.perf_counter() - start_time))
except (Exception, psycopg2.Error) as error:
print ("Error while fetching data from PostgreSQL", error)
print("Error adding information.")
quit()