简体   繁体   English

如何从csv或txt文件中删除标头?

[英]How can I remove header from csv or txt file?

I chose csv's specific column and converted it to txt. 我选择了csv的特定列并将其转换为txt。

The selected column name is 'id' (header) and I don't know how to remove that 'id' from txt. 所选的列名称为“ id”(标题),我不知道如何从txt中删除该“ id”。

This is my code.. 这是我的代码。

import csv
with open('garosu_example.csv') as file:
    reader = csv.reader(file)

input_file = "garosu_example.csv"
output_file = "garosu_example.txt"

id = ['id']
selected_column_index = []

with open(input_file, 'r', newline='') as csv_in_file:
    with open(output_file, 'w', newline='/n') as csv_out_file:
        freader = csv.reader(csv_in_file)
        fwriter = csv.writer(csv_out_file)
        header = next(freader)
        for index_value in range(len(header)):
            if header[index_value] in id:
                selected_column_index.append(index_value)
        print(id)
        fwriter.writerow(id)
        for row_list in freader:
            row_list_output = []
            for index_value in selected_column_index:
                row_list_output.append(row_list[index_value])
            print(row_list_output)
            f.writer.writerow(row_list_output)

How can I remove 'id'(header) from txt file? 如何从txt文件中删除“ id”(标题)?

If I remove header in csv, output txt file is emptyT_T 如果我删除csv中的标头,则输出txt文件为空T_T

这是我的csv文件的示例

Another way with readlines and writelines : 另一种方式与readlineswritelines

with open('filename.csv') as f:
    with open('no_header.csv', 'w') as f2:
        f2.writelines(f2.readlines()[1:])

You can skip the first line and replace the document with everything after: 您可以跳过第一行,然后将文档替换为所有内容:

with open(file) as f:
    with open("file_without_header.csv",'w') as nh:
        next(f)
        for l in f:
            nh.write(l)

Another way you can try is to use pandas to manipulate your csv file. 您可以尝试的另一种方法是使用熊猫来操作您的csv文件。

An example of using pandas to manipulate the csv file would be 使用熊猫操作csv文件的示例是

import pandas as pd

df = pd.read_csv("<Your-CSV-file>", columns=["id", "name", "longitude", "latitude"])
df.drop(columns=["id"])  ## this will drop the column named id
df.to_csv("<Your-New-CSV-file-Path>", index=None, header=True)

Hope it helps. 希望能帮助到你。

暂无
暂无

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

相关问题 如何将 UTF-16-LE txt 文件转换为 ANSI txt 文件并删除 PYTHON 中的 header? - How can i convert a UTF-16-LE txt file to an ANSI txt file and remove the header in PYTHON? 如何从 txt 文件中删除字符串? - How can I remove a string from a txt file? 如何使用 pandas 从 csv 文件中删除一行? - How can I remove a line from a csv file using pandas? 如何从 csv 文件中删除不必要的字符 - How can I remove unnecssary characters from a csv file 如何从 a.txt 文件中读取某些字符并将它们写入 Python 中的 a.csv 文件? - How can I read certain characters from a .txt file and write them to a .csv file in Python? 我想从我的csv编写器(输出文件)中删除标头 - I want to remove the header from my csv writer (the output file) 如何使用csv中的数据替换txt文件模板中的占位符? - How can I substitute placeholders in a txt file template using data from a csv? 如何允许用户使用 Json 到 Python 从 .txt 文件中删除 Object - How Can I Allow the User to Remove an Object From a .txt file Using Json through Python 如何保留从a.csv文件中删除第二列并将文件的rest保存到Python中的a.txt文件 - How to keep the remove the second column from a .csv file and save the rest of the file to a .txt file in Python 如何从csv读取没有标题的列并使用Python将输出保存在txt文件中? - How to read a column without header from csv and save the output in a txt file using Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM