English 中文(简体)
从位于Adhur的1 txt档案中开发一个LQite3数据库
原标题:Populating a SQLite3 database from a .txt file with Python

我正试图在django建立一个网站,使用户能够向一个数据库提出询问,其中包含他们在欧洲议会的代表情况。 我的数据是按ma顺序排列的。

议会,名称,国家,缔约方——团体,国家 缔约方,职位

7 Marta Andreasen,联合王国,欧洲自由与民主团体,联合王国独立党,成员

......

我想用这一数据充实一个KQite3数据库,但迄今为止,我发现的所有辅导员都只表明如何以手方式做到这一点。 自2006年以来 我在档案中确实想这样做。

我怀疑这是一个简单的问题,但如果有人能向我表明如何这样做,我将非常感谢。

Thomas

最佳回答

签署:models.py 看起来如此:

class Representative(models.Model):
    parliament = models.CharField(max_length=128)
    name = models.CharField(max_length=128)
    country = models.CharField(max_length=128)
    party_group = models.CharField(max_length=128)
    national_party = models.CharField(max_length=128)
    position = models.CharField(max_length=128)

然后可以管理<代码>python。 ll/条码:

import csv
from your_app.models import Representative
# If you re using different field names, change this list accordingly.
# The order must also match the column order in the CSV file.
fields = [ parliament ,  name ,  country ,  party_group ,  national_party ,  position ]
for row in csv.reader(open( your_file.csv )):
    Representative.objects.create(**dict(zip(fields, row)))

你也做了。

<>Addendum (edit)

Per Thomas s request, here s an Interpretation of what **dict(zip(fields,row)):

最初,<代码>fields 载有我们界定的外地名称清单,row载有一份价值清单,该清单代表了CSV档案中的目前行文。

fields = [ parliament ,  name ,  country , ...]
row = [ 7 ,  Marta Andreasen ,  United Kingdom , ...]

>>> zip(fields, row)
[( parliament ,  7 ), ( name ,  Marta Andreasen ), ( country ,  United Kingdom ), ...]

<代码>字典>功能只是将奶制品清单转换成字典。

>>> dict(zip(fields, row))
{ parliament :  7 ,  name :  Marta Andreasen ,  country :  United Kingdom , ...}

<代码>**>是将字典转换成功能的关键词表的一种方式。 www.un.org/spanish/ga/president 例如,电话create (**dict(zip(field, row)))相当于:

create(parliament= 7 , name= Marta Andreasen , country= United Kingdom , ...)

希望这种明亮点。

问题回答

As Siggy F 说,与Joschua相比,只有稍有不同:

1. 创建文本档案,备有您的图表,例如:

CREATE TABLE politicians (
    Parliament text, 
    Name text, 
    Country text, 
    Party_Group text, 
    National_Party text, 
    Position text
);

编制表格:

>>> import csv, sqlite3
>>> conn = sqlite3.connect( my.db )
>>> c = conn.cursor()
>>> with open( myschema.sql ) as f:            # read in schema file 
...   schema = f.read()
... 
>>> c.execute(schema)                          # create table per schema 
<sqlite3.Cursor object at 0x1392f50>
>>> conn.commit()                              # commit table creation

采用v模块,读取数据:

>>> csv_reader = csv.reader(open( myfile.txt ), skipinitialspace=True)
>>> csv_reader.next()                          # skip the first line in the file
[ Parliament ,  Name ,  Country , ...

# put all data in a tuple
# edit: decoding from utf-8 file to unicode
>>> to_db = tuple([i.decode( utf-8 ) for i in line] for line in csv_reader)
>>> to_db                                      # this will be inserted into table
[(u 7 , u Marta Andreasen , u United Kingdom , ...

插入数据:

>>> c.executemany("INSERT INTO politicians VALUES (?,?,?,?,?,?);", to_db)
<sqlite3.Cursor object at 0x1392f50>
>>> conn.commit()

2. 证明所有内容都符合预期:

>>> c.execute( SELECT * FROM politicians ).fetchall()
[(u 7 , u Marta Andreasen , u United Kingdom , ...

Edit:
And since you ve decoded (to unicode) on input, you need to be sure to encode on output.
For example:

with open( encoded_output.txt ,  w ) as f:
  for row in c.execute( SELECT * FROM politicians ).fetchall():
    for col in row:
      f.write(col.encode( utf-8 ))
      f.write( 
 )

您询问创建线(**dict(zip(fields,row)))的情况。

我不知道如何直接答复你的意见,因此我在此试图回答。

采用多种清单作为动力,并将对应要素清单作为标记。

zip (list1, list2) => [(list1 [0], list2[0], (list1 ***, list2], ......]

字典列出了2个元素,并将每个图一元素(钥匙)的字典图归第二要素(价值)。

创建职能需要关键词。 您可以使用** 左右的字典,将该词句变成关键词。

创造(**)名称:john , 年龄:5} => 创造(姓名=john , 年龄=5)

象以下几条工作一样:

# Open database (will be created if not exists)
conn = sqlite3.connect( /path/to/your_file.db )

c = conn.cursor()

# Create table
c.execute(   create table representatives
(parliament text, name text, country text, party_group text, national_party text, position text)   )

f = open("thefile.txt")
for i in f.readlines():
    # Insert a row of data
    c.execute("""insert into representatives
                 values (?,?,?,?,?,?)""", *i.split(", ")) # *i.split(", ") does unpack the list as arguments

# Save (commit) the changes
conn.commit()

# We can also close the cursor if we are done with it
c.close()

如果你想用一种简单的方法使用三利基,那么你可以采取这三种步骤:

$ sqlite3 db.sqlite3
sqlite> .separator ","
sqlite> .import myfile.txt table_name

然而,要牢记以下几点:

  1. The .txt file should be in the same directory as your db.sqlite3,
    otherwise use an absolute path "/path/myfile.txt" when importing
  2. Your schema for tables (number of columns) should match with the number of values seperated by commas in each row in the txt file

页: 1 页: 1

SQLite version 3.23.1 2018-04-10 17:39:29
Enter ".help" for usage hints.
sqlite> .tables
auth_group                  table_name               
auth_group_permissions      django_admin_log          
auth_permission             django_content_type       
auth_user                   django_migrations         
auth_user_groups            django_session            
auth_user_user_permissions




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签