1

I want to insert two columns in a new csv file. Question1 data should be in first column and Question2 data should be in second column

below given code gives me this output:

['question1']
['a','b','c','d']
['e','f','g']
['h','i','j','k','l']
['question2']
['a','b','c','d','x','y']
['e','f','g','m','n','o','p','q']
['h','i','j','k','l','r','s',]

Here is my code:

col1=question1.split("\n")
col2=question2.split("\n")
with open("outputFile.csv" , mode="wt", encoding='UTF-8') as out_file:
     w=csv.writer(out_file)
     for row in col1:
         myColumns = row.split("\n")
         print(myColumns)
         w.writerow(myColumns)
     for row in col2:
         myColumns = row.split("\n")
         print(myColumns)
         w.writerow(myColumns)

the output should be like this: question1 should be in first column of csv and question 2 should be in second column of csv file

['question1']   ['question2']
['a','b','c','d']  ['a','b','c','d','x','y']
['e','f','g']  ['e','f','g','m','n','o','p','q']
['h','i','j','k','l']  ['h','i','j','k','l','r','s',]

Please help me how can I solve the problem..

4
  • How should the output look? Commented Aug 18, 2017 at 12:20
  • i have added my desired output in post. Commented Aug 18, 2017 at 12:25
  • You'll also need to provide how your input actually looks. Is it question1 = [['What', 'step', 'step', 'guide', 'invest', 'share', 'market', 'india'],['What', 'story', 'Kohinoor', 'KohiNoor', 'Diamond'] ...] Do you need the entire list on one line? Or each list element on one line? Commented Aug 18, 2017 at 12:43
  • 1st column should contain question 1 and all its data, each element of list on separate line. similarly question 2 and its data should be in 2nd column. Commented Aug 18, 2017 at 13:00

2 Answers 2

5

You can use pandas for this.

import pandas as pd

question1 = [['1', '1'], ['1', '2', '3'], ['3', '4']]   #question 1 data
question2 = [['is', 'was'], ['i', 'am', 'me'],['yes', 'no']] #question 2 data

df = pd.DataFrame(columns=["question1", "question2"])
df["question1"] = question1
df["question2"] = question2

df.to_csv("output.csv", index=False)

output.csv

question1,question2
"['1', '1']","['is', 'was']"
"['1', '2', '3']","['i', 'am', 'me']"
"['3', '4']","['yes', 'no']"
Sign up to request clarification or add additional context in comments.

Comments

0

So as I understand it the output you want is something like this: [question1 data], [question2 data] [question1 data], [question2 data] ...

In that case the following will get you there:

first_column = [['a', 'b'], ['c', 'd', 'e'], ['f', 'g']]   #Populate this with your data from question 1
second_column = [['1', '2'], ['3', '4', '5']]              #Populate this with data from question 2


#Find the shortest and the longest of the lists
(shortest_list, lognest_list) =  (first_column, second_column) if len(first_column) < len(second_column) else (second_column,first_column)


#If the two colmns are guarenteed to be of the same length, then this loop is enough
for i in range(0,len(shortest_list)):
    print(str(first_column[i]) + ", " + str(second_column[i]))

#Handle that the who lists may not be of equal lengths
if len(shortest_list) != len(lognest_list):
    for i in range(len(shortest_list),  len(lognest_list)):
        print(str(lognest_list[i]) + ", ")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.