以csv正确格式

时间:2018-02-08 12:43:14

标签: python csv dictionary

我有字典项目有问题,答案0,答案-1等等,最多10个。答案数量从1-10开始

例如

{u'answer-0': u'Im the H4 visa holder (expiring Jun2018).\xa0 However I\u2019m a Class A misdemeanor, to offense theft of property on12/3/2015. And it was dismissed and seal in dec2016.Im in USA from April 2011. I have no other offence on my record.\xa0Can you please tell me what are my chances to get approved green card. My husband has approved I-140.\xa0Can I apply along with him as dependent?Cloud you please guide me to the process of application to avoid denial of GC. 0',
 u'question': u'GC with misdemeanor'}
{u'answer-0': u'My father in law is seeking re-entry into Canada to attend a funeral. My understandings is he would either have to file a I-131 re entry form or obtain us citizenship and get a passport. he would rather seek citizenship, but I guess our question\xa0would be can he gain citizenship in the\xa0us because he now has misdemeanors he drank and drove, And if he can not gain citizenship and we file the I-131 can he be denied entry back into the us because of the misdemeanors even though he lived ,worked, and retired in the us as a residential alien. We would appreciate any helpful information, his mother wants to be buried and funeral services in Canada\xa0but he cannot afford to loose his residency in our country\xa0because he built his life and family in the us.\xa0it also might\xa0be helpful to know what kind of information he will have to have in Oder to pass the citizenship test\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0 thanks Mat 0',
 u'answer-1': u'What is his current status and of which country is he currently a citizen? That is what will govern his entry to Canada and subsequently, back to the US. Citizenship is not required and certainly not something which can be obtained quickly. 0',
 u'answer-10': u"32 minutes ago, ElleMD said:For Resident Aliens, a green card will get them into Canada. http://www.cic.gc.ca/english/helpcentre/answer.asp?qnum=593&top=16Per the link, that's true if the resident alien drives (or takes a boat).\xa0 That's not true if he wants to fly to Canada -- in that circumstance a passport will be necessary.\xa0It also may matter how long he plans to stay in Canada. If more than 12 months, there would be some additional paperwork required if he wants to come back; and I think there may be some circumstances where staying more than 6 months also triggers some paperwork requirements.\xa0Also, I think we've all been overlooking part of the OP's question -- the effect of the resident alien's criminal record on his ability to re-enter the US. Depending on the nature of the crimes and the number of offenses, the resident alien might have a problem returning to the US.\xa0On 1/17/2018 at 0:32 PM, mat said:because he now has misdemeanors he drank and drove\xa0It would be a good idea to speak with an immigration attorney before he goes to Canada to get specific guidance on whether his particular criminal record might prevent re-entry.\xa0 There's not enough information in the OP's post to evaluate the risk.\xa0 0",
 u'answer-2': u'1 hour ago, mat said:it also might\xa0be helpful to know what kind of information he will have to have in Oder to pass the citizenship testhttps://www.uscis.gov/citizenship/learners/study-test\xa0This website has a lot of useful information about the English test and the civics test that are required as part of naturalization. 0',
 u'answer-3': u'Thank you for your responses we appreciate the feedback, I believe\xa0his current citizenship would be of Canada he was born in Canada and his family moved here to the usa when he was 8 yrs old in 1965, his current status would be resident alien in the united states I believe because\xa0that is was is on the top of his alien card. 0',
 u'answer-4': u'1 hour ago, ElleMD said:What is his current status and of which country is he currently a citizen? That is what will govern his entry to Canada and subsequently, back to the US. Citizenship is not required and certainly not something which can be obtained quickly.\xa01 hour ago, MiddlePart said:https://www.uscis.gov/citizenship/learners/study-test\xa0This website has a lot of useful information about the English test and the civics test that are required as part of naturalization.I believe he would be considered a Canadian citizen because he was born in that country he does have a resident alien card issued to him through the u.s department of justice-immigration. so I believe his current status would be resident alien to the u.s 0',
 u'answer-5': u'He can just use his "green card" to travel between the US and Canada. If he is Canadian, there is no issue with his entry into the country. At the US border, his green card would allow him to travel back into the US. No visa is needed. 0',
 u'answer-6': u'so he should not have a problem re- entering the us even though he has misdemeanors??\xa0I would like to thank middle part and Elle for their information. That they have shared.\xa0 0',
 u'answer-7': u"5 hours ago, mat said:\xa0\xa0My father in law is seeking re-entry into Canada to attend a funeral. My understandings is he would either have to file a I-131 re entry form or obtain us citizenship and get a passport.What he needs to get back into Canada is a Canadian passport.\xa0 The green card won't get him into Canada.\xa0 He should consult the nearest embassy or consular office for assistance.\xa0 0",
 u'answer-8': u'Having just been through the process, I can tell you that getting a Canadian passport from the US is not as easy as just showing up at an embassy or consular office. He is going to need a guarantor; someone who has a Canadian\xa0 passport and who has known him a minimum of two years. (It can be a family member.) He will have to have an application form and pictures of himself that are co-signed by that guarantor. In most cases it will take weeks if not months, and the only exceptions for fast-tracking it require that you have a flight leaving less than ten days AND you also have a valid passport from a visa-exempt country. 0',
 u'answer-9': u"20 hours ago, LegalwriterOne said:What he needs to get back into Canada is a Canadian passport.\xa0 The green card won't get him into Canada.\xa0 He should consult the nearest embassy or consular office for assistance.\xa0For Resident Aliens, a green card will get them into Canada. http://www.cic.gc.ca/english/helpcentre/answer.asp?qnum=593&top=16 0",
 u'question': u'my father in-law is a resident alien from canada to us hes been her 50+yrs'}
{u'answer-0': u'Married my husband that has a criminal record.. mainly misdemeanors..can I still help him get legal right to be in the US with me ?\U0001f62d 0',
 u'answer-1': u'We have no idea what abilities you have.\xa0 You and/or your husband should consult with an immigration attorney. 0',
 u'answer-2': u'Immigrant implies he is already here, in the US. Is he undocumented or does he have a visa/green card/etc.? If you meant he was a foreign national living outside the US and looking to move to the US, that is a different story. Are you a US citizen? You really need to speak with an immigration attorney. 0',
 u'question': u'Married a immigrant that has a criminal record'}
{u'answer-0': u'Hello ReAders,\xa0Thanks for your time.\xa0"WE", need some help for followings,viz;\xa01, \xa0POA = to be drafted in Gujarati and /or ENGLISH for all family inherited NRI\'s\xa0Land .....and to be done @\xa0Anand-Court.2. To find a " REVENUE- Lawyer in Anand area for further proceedings of LAND documents.\xa0Have a nice day!The Simple Guy\xa0\xa0\xa0 0',
 u'answer-1': u'Sorry, but this site only handles questions about law in the United States. Your references to Gujarati and Anand court tells me that the situation you have is in India, and for that you need to find a site that focuses on law in India.  0',
 u'question': u'A Gujarati Lawyer = a POA for adding and DELETING name/S on Land documents'}
{u'answer-0': u'Hello Readers,\xa0Thanks for your time!\xa0A few questions on I-90 FORM and ..........how to CORRECTLY answer them.How do you choose the followings to answer = item # 1,2, 3a and 3a1\xa0?\xa0Petition Filed in 1988 =\xa0 INS ,Westminster,CAVisa Interview \xa0 @= BOMBAYVisa Granted @ = BOMBAYPOE = JFK,New YorkDestination = Los Angeles,CA\xa0\xa0\xa0\xa0I-90\xa0 = Part 3. Processing Information\xa0Item Number 1. Location where you applied =---------------\xa0for an immigrant visa or adjustment of status.List the location of the U.S. Embassy, U.S. Consulate, or USCIS office where you filed your application for an immigrant visa or adjustment of status.\xa0Item Number 2. Location where your immigrant visa was issued =---------------------\xa0or USCIS office where you were granted adjustment of status. List the location of the U.S. Embassy, U.S. Consulate, or USCIS office where you were issued an immigrant visa or granted lawful permanent resident status.\xa0Item Numbers 3.a and 3.a.1. Complete Item Numbers 3.a. and 3.a.1. if you entered the United States with an immigrant visa.If you entered the United States with an immigrant visa, you must list your destination =------------------in the United States at time of admissionand provide the city or town and state of the POE =------------------- where you were admitted to the United States.Please note that some cities have several POEs. Indicate the type of POE where you were admitted to the United States, such as an airport, bridge, or tunnel.\xa0Have a nice day!The simple guy\xa0 0',
 u'question': u'I-90 and tricky Questions = For Green card Re-newal'}

我想在csv文件中写这样的

问题|回答0 |回答1 |回答2 |等等

我试过这种方式 -

    for item in result:
        # pprint.pprint(item)
        result_list.append(item)
        with open('result.csv', 'a') as csv_file:
            writer = csv.writer(csv_file)
            for key, value in item.items():
               writer.writerow([key.encode('utf-8'), value.encode('utf-8')])

哪个商店的结果如下

question
answer-0
answer-1
question
answer-0
question
answer-0
answer-1
answer-2
and so on 

如何在单行中编写每个字典项?

1 个答案:

答案 0 :(得分:2)

您应该将文件上下文保留为最外层。除此之外,您只需要从词典中生成正确排序的列表:

sort_func = lambda x: -1 if x[0] == 'question' else int(x[0].split('-')[1])

with open('result.csv', 'w') as csv_file:
    writer = csv.writer(csv_file)
    for entry in result:
        row = [val for key, val in sorted(entry.items(), key=sort_func)]
        writer.writerow(row)

我假设results是一个字典列表,其中每个条目都是questions, answer-0, answer-1, ..字典。