gpt4 book ai didi

python - 如何从 csv 或 txt 文件中删除标题?

转载 作者:太空宇宙 更新时间:2023-11-04 09:29:38 33 4
gpt4 key购买 nike

我选择了csv的特定列并将其转换为txt。

选择的列名是'id'(header)而且我不知道如何从 txt 中删除该“id”。

这是我的代码..

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)

如何从 txt 文件中删除“id”(标题)?

如果我删除 csv 中的标题,输出的 txt 文件为空T_T

Here is an example of my csv file

最佳答案

readlineswritelines 的另一种方式:

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

关于python - 如何从 csv 或 txt 文件中删除标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56213687/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com