Using pipe in a system call
Cecil Westerhof
Cecil at decebal.nl
Fri Oct 9 02:56:18 EDT 2015
On Friday 9 Oct 2015 02:11 CEST, Dennis Lee Bieber wrote:
> On Fri, 09 Oct 2015 00:03:36 +0200, Cecil Westerhof
> <Cecil at decebal.nl> declaimed the following:
>
>> I want to do the following Bash command in Python: sqlite3
>> spreekwoorden.sqlite "SELECT spreekwoord FROM spreekwoorden;" |
>> sort > spreekwoorden2.txt
>>
>> The following does this in Python:
>> sqlite_pipe = Popen(
>> (
>> 'sqlite3',
>> 'spreekwoorden.sqlite',
>> 'SELECT spreekwoord FROM spreekwoorden;'
>> ),
>> stdout = PIPE
>> )
>> Popen(
>> (
>> 'sort',
>> '--output=spreekwoorden2.txt',
>> ),
>> stdin = sqlite_pipe.stdout
>> )
>>
>> Is this the correct way, or is there a better way?
>>
>
> Use the standard db-api interface for SQLite3 provided with
> Python and avoid creating external processes?
>
> Heck, why rely on an external sort command when you can add
> "ORDER BY spreekwoord" to the SQL statement.
My bad, I intended to mention that ORDER BY gives the wrong order (é
comes after z and with sort it comes after e), so that is why I use
the external sort command.
> Test data (out of habit, I always create an autonumber primary
> key) (this is shown as CSV):
I am using that also, but when I add things to the database I just
want to have a sorted file with all the proverbs (that is the meaning
of spreekwoord) so I can use it to import everything again if need to
be.
The create statement:
CREATE TABLE spreekwoorden (
spreekwoordID INTEGER PRIMARY KEY AUTOINCREMENT,
spreekwoord TEXT UNIQUE NOT NULL,
gebruikt TEXT DEFAULT CURRENT_DATE NOT NULL
)
> fout = open("wordlist.txt", "w")
Fout means wrong in Dutch. ;-)
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
More information about the Python-list
mailing list