简体   繁体   English

to_csv 在每次向 csv 文件插入数据时存储列 label

[英]to_csv storing columns label on every insert of data to csv file

when i am inserting data first time in csv file it is good but on second time it again inserts column name当我第一次在 csv 文件中插入数据时它很好,但第二次它再次插入列名

import pandas as pd




name = input("Enter student name")

print("")
print("enter marks info below")
print("")
eng= input("enter English mark : ")
maths= input("enter Maths mark : ")
physics= input("enter Physics mark : ")
chemistry= input("enter Chemistry mark : ")
ip= input("enter Informatic Practices mark : ")

dict = {
        "name":{name:name},
        "english":{name:eng},
        "maths":{name:maths},
        "physics":{name:physics},
        "chemistry":{name:chemistry},
        "ip":{name:ip}
    }
df= pd.DataFrame(dict)


df.to_csv("hello.csv", sep="|",index=False,na_rep="null",mode='a')
print("hello.csv")
read = pd.read_csv("hello.csv", sep='|')

print(read)

data in csv file: csv 文件中的数据:

name|english|maths|physics|chemistry|ip
dddd|dd|e3|3|3|3
name|english|maths|physics|chemistry|ip 
ddddddd|e33|33|3||3
name|english|maths|physics|chemistry|ip
dddddd|33|333||3|

please help in solving how to fix so that column not get added multiple time请帮助解决如何修复该列不会被多次添加

you can read the csv file everytime before your run this script.您可以在每次运行此脚本之前阅读 csv 文件。

import pandas as pd
import os

df = pd.DataFrame() if not os.path.exists("hello.csv") else pd.read_csv("hello.csv", sep='|')

name = input("Enter student name")
print("")
print("enter marks info below")
print("")
eng = input("enter English mark : ")
maths = input("enter Maths mark : ")
physics = input("enter Physics mark : ")
chemistry = input("enter Chemistry mark : ")
ip = input("enter Informatic Practices mark : ")

dict = {
    "name": {name: name},
    "english": {name: eng},
    "maths": {name: maths},
    "physics": {name: physics},
    "chemistry": {name: chemistry},
    "ip": {name: ip}
}
df = df.append(pd.DataFrame(dict))

df.to_csv("hello.csv", sep="|", index=False, na_rep="null", mode='w')
print("hello.csv")
read = pd.read_csv("hello.csv", sep='|')

print(read)

and you can also use this code below to export df without columns, but may still have to check file existence or columns sequence first.您也可以使用下面的代码导出不带列的 df,但可能仍需先检查文件是否存在或列顺序。

df.to_csv('filename.csv', header=False, sep='|', mode='a')

The output file is being opened in append mode, and to_csv() will include the header row by default. output 文件在 append 模式下打开,默认情况下to_csv()将包含 header 行。 You could simply turn off the header:您可以简单地关闭 header:

df.to_csv("hello.csv", header=False, sep="|", index=False, na_rep="null", mode='a')

which will work, but your CSV file will not have a header row.这将起作用,但您的 CSV 文件将没有 header 行。 If you require the header then you could check whether the output CSV file already exists and disable headers if it does.如果您需要 header 那么您可以检查 output CSV 文件是否已经存在,如果存在则禁用标头。

One way is that suggested in the answer by @Eason , however, that might not be practical for large files due to the extra loading time (possibly this is the reason why you are using append mode?)一种方法是@Eason在答案中建议,但是,由于额外的加载时间,这对于大文件可能不实用(可能这就是您使用 append 模式的原因?)

The following disables the CSV headers if the file already exists and is not empty.如果文件已存在且不为空,则以下禁用 CSV 标头。

from pathlib import Path

header = True
p = Path('hello.csv')
if p.exists() and p.stat().st_size:
    header = False

df.to_csv(p, header=header, sep="|", index=False, na_rep="null", mode='a')

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

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