简体   繁体   English

Python + CSV:如何在Python中创建一个新的csv并为此写入新的列?

[英]Python + CSV : How can I create a new csv and write new columns to that in Python?

I want to make a new CSV from scratch. 我想从头开始制作新的 CSV。 In that CSV I'll store new cells row wise. 在该CSV中,我将逐行存储新单元格。 Each cell value will be computed dynamically and row will be stored to the csv in a loop. 每个单元格值将动态计算,并且行将循环存储到csv中。 Unfortunately, all of the available codes for this purpose are for already existing CSVs. 不幸的是,用于此目的的所有可用代码均适用于现有的CSV。 A code without using Pandas dataframe will be preferred. 最好使用不使用Pandas数据框的代码。

Final CSV should look like this: 最终CSV应该如下所示: 在此处输入图片说明

you can create your own csv file, here I would like to show you how you can create csv file with headers. 您可以创建自己的csv文件,在这里我想向您展示如何创建带标题的csv文件。

import csv
rowHeaders = ["Title", "Coupon code", "Description", "Image path", "Website link", "offer expire"]
fp = open('groupon_output.csv', 'w')
mycsv = csv.DictWriter(fp, fieldnames=rowHeaders)
#write header will write your desire header
mycsv.writeheader()

# you can write multiple value to take it inside the loop
#you can write row values using dict writer
title="testing"
coupon_code="xx"
description="nothing much"
image_path="not given"
current_page_url="www.google.com"

mycsv.writerow({"Title": title, "Coupon code": coupon_code, "Description": description,"Image path": image_path, "Website link": current_page_url,"offer expire": "Not Avialable"})

import csv

data =  [
        [ 'a','b','c','d'],
        [ 'b','1','2','3'],
        [ 'c','4','5','6'],
        [ 'd','7','8','9'],
        ]

with open ('output.csv', 'w') as output:
    writer = csv.writer(output)
    writer.writerows(data)

if you have data coming in as a list , 如果您将数据作为列表输入,

import csv

list_1 = ['UOM','BelRd(D2)','Ulsoor(D2)','Chrch(D2)','BlrClub(D2)','Indrangr(D1)','Krmngl(D1','KrmnglBkry(D1)']
list_2 = ['PKT',0,0,0,0,0,0,1]

with open('/path/filename.csv', 'w', newline='') as outfile:
    writer = csv.writer(outfile)
    writer.writerow(list_1)
    writer.writerow(list_2)

This could help you! 这可以帮助您!

def header():
    # Instead of hard coding like below, pass variables which hold dynamic values
    # This kind of hard coding can you help you when headers are fixed
    h1 = ['Date']  
    h2 = ['Feed']
    h3 = ['Status']
    h4 = ['Path']

    rows = zip(h1, h2, h3, h4)

    with open('test.txt', 'a') as f:
        wr = csv.writer(f)
        for row in rows:
            wr.writerow(row)
        f.close()

暂无
暂无

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

相关问题 如何在python中将新列追加到csv中? - How can I append new columns into csv in python? 如何将CSV文件转换成Python中的列表,然后将列表数据写入新的CSV文件? - How can I convert a CSV file into a list in Python and then write the list data to a new CSV file? Python CSV读取文件并选择列并写入新的CSV文件 - Python CSV read file and select columns and write to new CSV file 如何获取 csv 列中的出现次数并保存为包含 python 中的计数的新 csv - How can I get a count of occurrances in csv columns and save as a new csv containing the count in python 如何在python中“写入新的.CSV文件”或“另存为新的.CSV文件” - How To ' Write To New .CSV File' or "Save As New .CSV File' In python 如何使用 Python 在 CSV 中写入新行 - How to write new rows in a CSV using Python Python:如何在新的CSV文件中写入两个CSV文件(其中一个具有列子集)的差异 - Python: How to write the difference of two CSV files, with one having a subset of columns, in a new CSV file Python,使用多个列表中的列将新行写入CSV - Python, Write new line to CSV using columns from multiple lists 尝试从 CSV 文件中获取信息,重新排列列,然后将新输出写入 Python 中的新 CSV 文件 - Trying to take info from a CSV file, rearrange the columns and then write the new output to a new CSV file in Python 使用 python,如何将新项目写入 CSV 文件中的新行 - Using python, how to write new item into a new row in a CSV file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM