简体   繁体   English

如何使用python在CSV文件中插入两列?

[英]How to insert two columns in a CSV File using python?

I want to insert two columns in a new csv file. 我想在新的csv文件中插入两列。 Question1 data should be in first column and Question2 data should be in second column Question1数据应在第一列中,而Question2数据应在第二列中

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应该在csv的第一列中,而question 2应该在csv文件的第二列中

['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.. 请帮助我如何解决问题。

You can use pandas for this. 您可以为此使用pandas

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 output.csv

question1,question2
"['1', '1']","['is', 'was']"
"['1', '2', '3']","['i', 'am', 'me']"
"['3', '4']","['yes', 'no']"

So as I understand it the output you want is something like this: [question1 data], [question2 data] [question1 data], [question2 data] ... 因此,据我了解,您想要的输出是这样的:[question1数据],[question2数据] [question1数据],[question2数据] ...

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]) + ", ")

暂无
暂无

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

相关问题 如何使用Python 3在两列中将值写入CSV文件? - How to write values to CSV file in two columns using Python 3? 如何使用 python 打印 a.csv 文件两列的值 - How to print values for two columns of a .csv file using python 如何通过python中的两列对csv文件进行排序? - how to sort a csv file by two columns in python? 如何使用python将文本文件中的两列提取到csv文件中? - How to extract two columns from a text file into a csv file using python? 如何使用python_列将csv文件插入数据库 - How to Insert csv files to database using python_ columns 如何仅排序和存储两个.CSV 文件中的前 3 个位置,然后使用 Python 将它们存储到一个.CSV 文件中的两列? - How to sort and store only the top 3 locations from two .CSV files and then store them into two columns in one .CSV file using Python? 使用 python 中的一个 csv 文件比较一列中的两个数据 - compare two data in a columns using one csv file in python 如何使用 Python 将 a.csv 文件中的两列(简单的乘法和加法)组合成新的列? - How to combine two columns (a simple multiplication and addition) from a .csv file to a new one using Python? 如何使用python获取csv文件中两个相邻列之间的差异? - How to get the difference between two adjacent columns in csv file using python? 如何在python中从csv文件绘制两个特定的列 - How to plot two specific columns from a csv file in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM