简体   繁体   English

在python中导入csv数据

[英]importing csv data in python

im am trying to import data into database from a csv downloaded from URL. 即时通讯正在尝试从从URL下载的csv将数据导入数据库。 The csv is 100MB large. CSV大小为100MB。

def get_csv_data():
   url = 'http://dati.ur.gov.lv/register/register.csv'
   response = urllib2.urlopen(url)

   cr = csv.reader(response, delimiter=';', quotechar='"')

   for row in cr:
       if row[0] != 'regcode':
          print row[2]
          ur = Ur()
          ur.regcode=row[0]
          ur.sepa=row[1]
          ur.name=row[2]
          ur.name_before_quotes=row[3]
          ur.name_in_quotes=row[4]
          ur.name_after_quotes=row[5]
          ur.without_quotes=row[6]
          ur.regtype=row[7]
          ur.regtype_text=row[8]
          ur.type=row[9]
          ur.type_text=row[10]
          ur.registered=row[11]
          ur.terminated=row[12]
          ur.closed=row[13]
          ur.address=row[14]
          ur.adressid=row[15]
          ur.region=row[16]
          ur.city=row[17]
          ur.atvk=row[18]
          ur.reregistration_term=row[19]
          ur.uri=row[20]
          ur.save()

I go through only two rows and then get a 500 error code. 我只经过两行,然后得到500错误代码。 whats wrong and how should i do this? 怎么了,我该怎么办?

A 500 HTTP response code is a problem with the server not on your end: 500 HTTP响应代码是服务器不在您端的问题:

Response status codes beginning with the digit "5" indicate cases in which the server is aware that it has erred or is incapable of performing the request. 以数字“ 5”开头的响应状态代码表示服务器知道服务器已出错或无法执行请求的情况。 Except when responding to a HEAD request, the server SHOULD include an entity containing an explanation of the error situation, and whether it is a temporary or permanent condition. 除响应HEAD请求外,服务器应包含一个实体,该实体包含错误情况的说明,以及它是暂时还是永久的情况。 User agents SHOULD display any included entity to the user. 用户代理应该向用户显示任何包含的实体。 These response codes are applicable to any request method. 这些响应代码适用于任何请求方法。

FWIW, it may work out better for you to pull down the entires page with a page = response.read() and then loop over the lines after retrieval using cr = csv.reader(page.splitlines(), delimiter=';', quotechar='"') . FWIW,对您来说,使用page = response.read()下拉整个页面,然后在使用cr = csv.reader(page.splitlines(), delimiter=';', quotechar='"')

500 is Internal Server Error, which means either the security settings are blocking the access or the database is corrupt and you are not able to read. 500是内部服务器错误,这意味着安全设置阻止了访问或数据库已损坏,您无法读取。 Check the security settings and see if you are able to query the DB through website. 检查安全设置,看看是否能够通过网站查询数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM