我有一个使用 Python 创建的文件,我想将其中的信息发送到数据库。我无法弄清楚如何使用 Python 而不是使用 PHP 脚本来完成此过程。这就是我目前使用 PHP 的方式......
$dbc = mysqli_connect('localhost', 'user', 'pass', 'db')
or die('Error connecting to MySQL server.');
$file_handle = fopen("output.txt", "r");
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode(' ', $line_of_text);
$FIRST = $parts[0];
$SECOND = $parts[1];
$THIRD = $parts[2];
$query = "INSERT INTO table (One, Two, Three)" .
"VALUES ('$FIRST', '$SECOND', '$THIRD')";
$result = mysqli_query($dbc, $query);
or die('Error querying database.');
}
fclose($file_hadle);
mysqli_close($dbc);
我想要使用的文件是一个文本文件,其中包含一堆采用这种格式的行
105a9afg0e8bc456egf9bc9852abcd78 00.1.1.2 Example
105a9afg0e8bc456egf9bc9852abcd78 00.1.1.2 Example2
我有 MySQLdb 来处理与数据库的连接。
最佳答案
你可以尝试:
>>> File = open("Foo", "r")
>>> for i in File.readlines(): print i.rstrip().split()
...
['105a9afg0e8bc456egf9bc9852abcd78', '00.1.1.2', 'Example']
['105a9afg0e8bc456egf9bc9852abcd78', '00.1.1.2', 'Example2']
关于php - Python 将文件行分成列表以发送到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25281828/