简体   繁体   English

在 python 中打印 csv 文件

[英]Printing a csv file in python

Data数据

Data - [DataFrame][1]

Hello there.你好呀。 I am a little newbie in python.我是 python 的小新手。 I need to edit the dataset in the picture.我需要编辑图片中的数据集。 But since there are 22 million lines and it is forcing my computer, I wanted to ask you before I tried it.但是因为有2200万行,而且是逼迫我的电脑,所以在我尝试之前我想问问你。 I want to print XYZ- "class" - "tahmin" columns from this data set (.csv file) to a new ".csv" file.我想将此数据集(.csv 文件)中的 XYZ-“类”-“tahmin”列打印到新的“.csv”文件中。 Can you help with this?你能帮忙吗?

I wish you healthy days:)祝你身体健康:)

I don't know what is in your.csv file, but according to this documentaion , something like this should works:我不知道你的.csv 文件中有什么,但根据这个文档,这样的东西应该有效:

import csv
with open('file.csv', 'r') as csvfile:
    with open('newfile.csv', 'w') as newfile:
        data = csv.reader(csvfile)
        writer = csv.writer(newfile)
        writer.writerow(["X", "Y", "Z", "class", "tahmin"])
        for row in data:
            writer.writerow(row)

you should share some code i think, not all 22 million lines of course, but enough for us to know what we're working.你应该分享一些我认为的代码,当然不是全部 2200 万行,但足以让我们知道我们在做什么。

In the meantime, please refer to this page explaining everything you need on reading/writing from cvs files in python.同时,请参阅此页面,解释从 python 中的 cvs 文件读取/写入所需的一切。

just to clarify, the page is from the official python documentation website.澄清一下,该页面来自官方 python 文档网站。 Goodluck.祝你好运。

use pandas https://pandas.pydata.org/使用 pandas https://pandas.pydata.org/

import pandas as pd 
dataset = pd.read_csv('file.csv')
dataset = dataset[['X','Y','Z', 'class' , 'tahmin']]
dataset.to_csv('newFile.csv')

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

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