我该如何仅在CSV文件python 3中删除标头开头和结尾的引号?
只是标题。
"WD_Code", "_Supplier_Number", "Date_Of_Snapshot", "Time_Of_Snapshot"
"data1", "data2", "data3", "data4"
使用熊猫将文件file.to_csv写入时可以做到吗?
答案 0 :(得分:0)
标题只是您可以通过熊猫访问的列表。您还可以根据需要重置该列表。在这里,我们将循环浏览文件中已存在的标头列表,并删除引号,然后将新列表重新分配为标头。
import pandas as pd
data = pd.read_csv("your_csv_here.csv")
new_headers = []
for header in data.columns: # data.columns is your list of headers
header = header.strip('"') # Remove the quotes off each header
new_headers.append(header) # Save the new strings without the quotes
data.columns = new_headers # Replace the old headers with the new list
在file.to_csv("your_csv_here.csv")
函数中无法完成此操作。
答案 1 :(得分:0)
尝试一下
headers = ["WD_Code", "_Supplier_Number", "Date_Of_Snapshot", "Time_Of_Snapshot"
"data1", "data2", "data3", "data4"]
headers_no_quotes = []
for header in headers:
headers_no_quotes.append(header.strip('\"'))