简体   繁体   English

如何将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?

I have a csv file with some lines(couple row of text), and I am trying to make a list from the csv file and write a csv file with a "Text" header.我有一个包含几行(几行文本)的 csv 文件,我正在尝试从 csv 文件中列出一个列表,并编写一个带有“文本”header 的 csv 文件。

I am trying like this我正在尝试这样

import csv

crimefile = open('file_1.csv', 'r')
reader = csv.reader(crimefile)
allRows = [row for row in reader]
print(allRows)

It's output like that:就是这样的 output:

[["Line a 9x9 inch baking dish with lightly greased foil or plastic wrap. Grease another piece of foil or plastic wrap to cover the top, and set aside.\nPlace 1/2 cup of water in the bowl of an electric mixer, and sprinkle gelatin on top of water to soak.\nWhile gelatin is soaking, combine 1/4 cup of water, corn syrup, and sugar in a saucepan, and bring to a boil over medium heat. Boil the mixture hard for 1 minute.\nPour the hot sugar mixture into the gelatin mixture and beat on high for 12 minutes with electric mixer, until the mixture is fluffy and forms stiff peaks. Add vanilla and peppermint extracts, and beat just until blended.\nPour the marshmallow mixture into the prepared baking dish, using a greased spatula to smooth the top of the candy. Cover the candy with the reserved greased foil or wrap, and press down lightly to seal the covering to the top of the candy.\nAllow the marshmallow candy to rest for 4 hours or overnight. Mix together cornstarch and confe [[“在一个 9x9 英寸的烤盘上涂上少许油脂的箔纸或保鲜膜。在另一张箔纸或保鲜膜上涂上油脂,盖住顶部,然后放在一边。\n将 1/2 杯水放入电动搅拌机的碗中,将明胶撒在水上浸泡。\n在浸泡明胶的同时,将 1/4 杯水、玉米糖浆和糖放入平底锅中,用中火煮沸。将混合物煮沸 1 分钟。 \n将热糖混合物倒入明胶混合物中,用电动搅拌器高速搅拌 12 分钟,直到混合物呈蓬松状并形成 forms 硬峰。加入香草和薄荷提取物,搅拌直至混合。\n将棉花糖混合物倒入准备好的烤盘,用涂了油的抹刀抹平糖果的顶部。用预留的涂油箔纸或包装纸盖住糖果,轻轻按下以将覆盖物密封到糖果顶部。\n让棉花糖糖到 rest 4 小时或过夜。混合玉米淀粉和咖啡ctioner's sugar in a shallow dish.浅盘中的糖粉。 Using oiled scissors or an oiled kitchen knife, cut the marshmallow candy into strips, then into 1 inch squares.使用上油的剪刀或上油的菜刀,将棉花糖切成条状,然后切成 1 英寸的正方形。 Dredge the marshmallows lightly in the cornstarch mixture and store in an airtight container."]]将棉花糖轻轻浸入玉米淀粉混合物中,然后储存在密封容器中。”]]

But i want like this:但我想要这样:

['Line a 9x9 inch baking dish with lightly greased foil or plastic wrap', 'Grease another piece of foil or plastic wrap to cover the top', 'and set aside', 'Place 1/2 cup of water in the bowl of an electric mixer', 'and sprinkle gelatin on top of water to soak', 'While gelatin is soaking', 'combine 1/4 cup of water', 'corn syrup, and sugar in a saucepan', 'and bring to a boil over medium heat', 'Boil the mixture hard for 1 minute', 'Pour the hot sugar mixture into the gelatin mixture and beat on high for 12 minutes with electric mixer', 'until the mixture is fluffy and forms stiff peaks', 'Add vanilla and peppermint extracts', 'and beat just until blended'] ['在 9x9 英寸的烤盘上涂上少许油脂的箔纸或保鲜膜','在另一张箔纸或保鲜膜上涂上油脂','然后放在一边','将 1/2 杯水放入碗中一个电动搅拌器”,“然后将明胶撒在水上浸泡”,“当明胶浸泡时”,“将 1/4 杯水混合”,“将玉米糖浆和糖放入平底锅中”,“然后将其放入中火煮沸', '将混合物用力煮沸 1 分钟', '将热糖混合物倒入吉利丁混合物中,用电动搅拌器高速搅拌 12 分钟', '直到混合物变得蓬松并出现 forms 个硬峰', '加入香草和薄荷提取物','然后搅拌直至混合']

It looks like you want the text to be split by commas and new lines, but all in one list.看起来您希望文本以逗号和换行符分隔,但都在一个列表中。

allRows = []
with open('file_1.csv', 'r') as crimefile:
    for line in crimefile:
        allRows += line.strip().split(',')
print(allRows)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM