擅长:python、mysql、java
<p>您可以使用<code>subprocess.run()</code>执行shell命令,以便在管道中使用Postgressql<code>COPY</code>将csv文件批量复制到服务器。我更熟悉<code>mssql</code>,它有<code>bcp</code>方法,无法在我的解决方案上完全测试,尽管我认为它是通过终端调用的类似方法。Terminal命令基于使用该方法的第三个链接,尽管该解决方案使用了<code>subprocess.call()</code>,该方法后来已用<code>subprocess.run()</code>更新</p>
<p><a href="https://docs.python.org/3/library/subprocess.html#subprocess.run" rel="nofollow noreferrer">https://docs.python.org/3/library/subprocess.html#subprocess.run</a>
<a href="https://ieftimov.com/post/postgresql-copy/" rel="nofollow noreferrer">https://ieftimov.com/post/postgresql-copy/</a></p>
<p><a href="https://stackoverflow.com/questions/46758865/python-psql-copy-csv-to-remote-server">Python psql \copy CSV to remote server</a></p>
<pre><code>import subprocess
psql_command = "\"\copy table (col1, col2) FROM file_location CSV HEADER QUOTE '\\\"' NULL ''\""
# user, hostname, password, dbname all defined elsewhere above.
command = ["psql",
"-U", user,
"-h", hostname,
"-d", dbname,
"-w", password,
"-c", psql_command,
]
subprocess.run(command)
</code></pre>