我需要读入1048行csv数据。数据如下所示:
\N,Olenevka,Оленевка,2596
\N,Urzuf,Урзуф,2904
\N,Lebedyn,ЛЕБЕДИН,27695
\N,Staryy Dobrotvor,Staryy Dobrotvor,6436
这是表格:
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| city_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | text | YES | | NULL | |
| alt_name | text | YES | | NULL | |
| population | int(10) unsigned | NO | | NULL | |
+------------+------------------+------+-----+---------+----------------+
以下是代码:
db.query("""
LOAD DATA INFILE '%s'
INTO TABLE %s
CHARACTER SET utf8
FIELDS TERMINATED BY ','
(@skip, name, alt_name, population)
""" % (temp_file, table))
class Database(object):
def __init__(self, database):
self.database = database
def query(self, cmd):
return subprocess.call(['mysql -u root -e "%s" %s' % (cmd, self.database)], shell=True)
当我从python发出命令时,它只读取1025行。但是,如果我从mysql提示符发出相同的命令,我会全部收到它们。
以下是运行代码后SELECT *的最后几行:
| 1023 | Alchevs’k | Алчевск | 116000 |
| 1024 | Yakymivka | Якимівка | 12353 |
| 1025 | Okhtyrka | Охтирка | 49818 |
| 1026 | Adzhamka | А | 0 |
+---------+------------------+-------------------------------------+------------+
最后一行只是部分阅读。这是文件的其余部分。
\N,Okhtyrka,Охтирка,49818
\N,Adzhamka,Аджамка,3859
\N,Malynivka,Малинівка,7502
\N,Mykhaylivka,Mykhaylivka,3352
\N,Kopashnovo,Копашново,3010
\N,Narkevychi,Наркевичі,1631
\N,Kirove,Кірове,3291
\N,Orikhove,Оріхове,3000
\N,Krasni Okny,Красні Окни,5461
\N,Pavlivka,Pavlovka,738
\N,Kuchurhan,Кучурган,3183
\N,Smyga,Смыга,2800
\N,Tayirove,Таїрове,1849
\N,Teplodar,Теплодар,8502
\N,Komsomolsk,Комсомольськ,51740
\N,Славутич,Славутич,25000
\N,Розгирче,Розгирче,566
\N,Orzhiv,Оржів,4125
\N,Shkil’ne,Шкільне,2127
\N,Denyshi,Деныши,1164
\N,Chetfalva,Четфалва,755
\N,Stepove,Степовое,1549
\N,Scholkine,,11677
\N,Yuzhnoukrains'k,Южноукраїнськ,39430
想法?
答案 0 :(得分:0)
尝试在每行的末尾添加反斜杠,如下所示:
db.query("""\
LOAD DATA INFILE '%s' \
INTO TABLE %s \
CHARACTER SET utf8 \
FIELDS TERMINATED BY ',' \
(null, name, alt_name, population) \
""" % (temp_file, table))
如果这不起作用,请尝试从“subprocess.call”调用中禁用shell处理。
例如:
壳=假