我想导入两种CSV文件,有些使用";" 用于分隔符和其他人使用",".到目前为止,我一直在接下来的两行之间切换:
reader=csv.reader(f,delimiter=';')
Run Code Online (Sandbox Code Playgroud)
要么
reader=csv.reader(f,delimiter=',')
Run Code Online (Sandbox Code Playgroud)
是否有可能不指定分隔符并让程序检查正确的分隔符?
下面的解决方案(Blender和sharth)似乎适用于以逗号分隔的文件(使用Libroffice生成),但不适用于以分号分隔的文件(使用MS Office生成).以下是一个以分号分隔的文件的第一行:
ReleveAnnee;ReleveMois;NoOrdre;TitreRMC;AdopCSRegleVote;AdopCSAbs;AdoptCSContre;NoCELEX;ProposAnnee;ProposChrono;ProposOrigine;NoUniqueAnnee;NoUniqueType;NoUniqueChrono;PropoSplittee;Suite2LecturePE;Council PATH;Notes
1999;1;1;1999/83/EC: Council Decision of 18 January 1999 authorising the Kingdom of Denmark to apply or to continue to apply reductions in, or exemptions from, excise duties on certain mineral oils used for specific purposes, in accordance with the procedure provided for in Article 8(4) of Directive 92/81/EEC;U;;;31999D0083;1998;577;COM;NULL;CS;NULL;;;;Propos* are missing on Celex document
1999;1;2;1999/81/EC: Council Decision of 18 January 1999 authorising the Kingdom of Spain to apply a measure derogating from …Run Code Online (Sandbox Code Playgroud) 关于模型的清理方法,我有两个问题.这是我的例子:
class AddProfileForm(ModelForm):
...
password = forms.CharField(max_length=30,widget=forms.PasswordInput(attrs={'class':'form2'}))
password_verify = forms.CharField(max_length=30,widget=forms.PasswordInput(attrs={'class':'form2'}), label='Retype password')
...
class Meta:
model = UserModel
fields=("username", "password", "password_verify", "first_name", "last_name", "date_of_birth", "biography", "contacts", )
#called on validation of the form
def clean(self):
#run the standard clean method first
cleaned_data=super(AddProfileForm, self).clean()
password = cleaned_data.get("password")
password_verify = cleaned_data.get("password_verify")
#check if passwords are entered and match
if password and password_verify and password==password_verify:
print "pwd ok"
else:
raise forms.ValidationError("Passwords do not match!")
#always return the cleaned data
return cleaned_data
Run Code Online (Sandbox Code Playgroud)
我应该一直打电话给标准清洁方法吗?
cleaned_data=super(AddProfileForm, …Run Code Online (Sandbox Code Playgroud)我在Hive中创建外部表的结构:
CREATE EXTERNAL TABLE google_analytics(
`session` INT)
PARTITIONED BY (date_string string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
LOCATION '/flumania/google_analytics';
ALTER TABLE google_analytics ADD PARTITION (date_string = '2016-09-06') LOCATION '/flumania/google_analytics';
Run Code Online (Sandbox Code Playgroud)
由于它是一个外部表,数据插入应该自动完成,对吧?
我的 django 表单集中有验证错误。从数据库填充的两个下拉列表没有通过验证,我不明白我的错误是什么。

型号:
class Country(models.Model):
country_code=models.CharField(max_length=2, primary_key=True)
country=models.CharField(max_length=20, unique=True)
def __unicode__(self):
return u"%s" % self.country
class Status(models.Model):
verbatim = models.ForeignKey(Verbatim)
country = models.ForeignKey(Country)
status = models.CharField(max_length=5, db_index=True)
def __unicode__(self):
return u"%s" % self.status
class Meta:
unique_together=(("verbatim", "country"), )
class ImportMinAttend(models.Model):
country=models.CharField(max_length=2, blank=False, null=False)
verbatim=models.CharField(max_length=250, blank=False, null=False)
status=models.CharField(max_length=5, blank=True, null=True, default=None)
Run Code Online (Sandbox Code Playgroud)
形式:
class MinAttendForm(forms.ModelForm):
country=forms.ModelChoiceField(queryset=Country.objects.all(), empty_label="Select a country")
status=forms.ModelChoiceField(queryset=Status.objects.values_list('status', flat = True).distinct(), empty_label="Select a status")
class Meta:
model=ImportMinAttend
#fields used for the validation
fields = ('country', 'verbatim', 'status') …Run Code Online (Sandbox Code Playgroud) 我正在尝试解析文档http://www.consilium.europa.eu/uedocs/cms_data/docs/pressdata/en/ecofin/acf8e.htm.我想在文档的开头提供国家/地区和名称.
这是我的代码
import urllib
import re
from bs4 import BeautifulSoup
url="http://www.consilium.europa.eu/uedocs/cms_data/docs/pressdata/en/ecofin/acf8e.htm"
soup=BeautifulSoup(urllib.urlopen(url))
attendances_table=soup.find("table", {"width":850})
print attendances_table #this works, I see the whole table
print attendances_table.find_all("tr")
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
AttributeError: 'NoneType' object has no attribute 'next_element'
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用与此帖相同的解决方案(我知道,再次,我:p): 带有无效html文档的beautifulsoup
我换了一行:
soup=BeautifulSoup(urllib.urlopen(url))
Run Code Online (Sandbox Code Playgroud)
有:
return BeautifulSoup(html, 'html.parser')
Run Code Online (Sandbox Code Playgroud)
如果我这样做:
print attendances_table
Run Code Online (Sandbox Code Playgroud)
我只得到:
<table border="0" cellpadding="10" cellspacing="0" width="850">
<tr><td valign="TOP" width="42%">
<p><b><u>Belgium</u></b></p></td></tr></table>
Run Code Online (Sandbox Code Playgroud)
我应该改变什么?
是否可以使用表单中的FileField上传django中的文件,但没有模型?到目前为止,我只能找到一个模型的例子.我不想在我的数据库中为它创建一个表,我只想上传一个文件.我的表格:
class csvUploadForm(forms.Form):
csvFile = forms.FileField(label='Select a CSV file to upload.', help_text='help')
Run Code Online (Sandbox Code Playgroud)
谢谢,
罗曼
我试图将我的表单序列化为json格式.我的看法:
form = CSVUploadForm(request.POST, request.FILES)
data_to_json={}
data_to_json = simplejson.dumps(form.__dict__)
return HttpResponse(data_to_json, mimetype='application/json')
Run Code Online (Sandbox Code Playgroud)
我有错误<class 'django.forms.util.ErrorList'> is not JSON serializable.如何处理django表格?
我有以下代码的语法错误:
ids_row={}
ids_row["releve_annee"]=int(row[0])
ids_row["releve_mois"]=int(row[1])
ids_row["no_ordre"]=int(row[2])
defaults={}
defaults["titre_rmc"]=row[3].strip()
defaults["notes"]=row[17].strip()
instance, created = Act.objects.get_or_create(**ids_row, defaults=defaults)
Run Code Online (Sandbox Code Playgroud)
我的模特:
class Act(models.Model):
releve_annee=models.IntegerField(max_length=4, blank=False, null=False)
releve_mois=models.IntegerField(max_length=2, blank=False, null=False)
no_ordre=models.IntegerField(max_length=2, blank=False, null=False)
titre_rmc=models.CharField(max_length=2000, blank=False, null=False)
notes=models.CharField(max_length=2000, blank=True, null=True, default=None)
class Meta:
unique_together=(("releve_annee", "releve_mois", "no_ordre"), )
Run Code Online (Sandbox Code Playgroud)
在一个shell中:
instance, created = Act.objects.get_or_create(**ids_row, defaults=defaults)
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
如果我删除defaults=defaults,错误消失.怎么了?
I have 4 variables:
I want to compute the average of the ratio:
(amdt_adopt - com_amdt_adopt) / (amdt_tabled - com_amdt_tabled)
Run Code Online (Sandbox Code Playgroud)
If I use the built-in avg function:
select avg((amdt_adopt - com_amdt_adopt) / (amdt_tabled - com_amdt_tabled)) as final_res
from europolix.act_act
where validated=2 and com_amdt_adopt>0 and amdt_adopt>0 and com_amdt_tabled>0 and amdt_tabled>0;
Run Code Online (Sandbox Code Playgroud)
I get 0.44771942.
If I use a sum and then divides by count:
select
(
select sum((amdt_adopt - com_amdt_adopt) / (amdt_tabled - com_amdt_tabled))
from europolix.act_act …Run Code Online (Sandbox Code Playgroud) django ×5
file ×2
forms ×2
python ×2
apache-pig ×1
average ×1
cloudera-cdh ×1
count ×1
csv ×1
delimiter ×1
dictionary ×1
django-forms ×1
external ×1
hdfs ×1
hive ×1
html ×1
html-parsing ×1
hue ×1
import ×1
json ×1
modelform ×1
mysql ×1
overriding ×1
parameters ×1
parsing ×1
partition ×1
sum ×1
upload ×1
validation ×1