有没有办法在保留行颜色的同时将行附加到现有的 csv 文件?
Is there a way to append rows to an existing csv file while retaining the colors of rows?
假设我有一个 csv 文件并且我更改了一些行的颜色。现在我有一个从 Internet 下载的新 csv 文件。它有一些新行和一些旧行。我想在新文件中找到唯一数据并将其附加到旧文件,同时保留所有颜色更改。
我可以做其他所有事情,但颜色正在重置。这是我正在做的。
old_data = pd.read_csv('old_data.csv')
new_data = pd.read_csv('new_data.csv')
unique_data = new_data[~new_data['Booking Reference'].isin(old_data['Booking Reference'])]
unique_data.to_csv('old_data.csv', 'a')
这样做会重置颜色。有没有办法保留这些信息?
非常感谢任何帮助。
如果您希望将新行附加到 Python 中的 CSV 文件中,您可以使用以下任何一种方法。
将所需行的数据分配到列表中。然后,使用 writer.writerow() 将此列表的数据附加到 CSV 文件。
将所需行的数据分配到字典中。然后,使用 DictWriter.writerow().
将此字典的数据附加到 CSV 文件中
我找到了一个符合您问题的示例。代码如下:
# Pre-requisite - Import the writer class from the csv module
from csv import writer
# The data assigned to the list
list_data=['03','Smith','Science']
# Pre-requisite - The CSV file should be manually closed before running this code.
# First, open the old CSV file in append mode, hence mentioned as 'a'
# Then, for the CSV file, create a file object
with open('CSVFILE.csv', 'a', newline='') as f_object:
# Pass the CSV file object to the writer() function
writer_object = writer(f_object)
# Result - a writer object
# Pass the data in the list as an argument into the writerow() function
writer_object.writerow(list_data)
# Close the file object
f_object.close()
在运行以上代码之前:
ID,NAME,SUBJECT
01,Henry,Python
02,Alice,C++
在运行上面的代码之后:
ID,NAME,SUBJECT
01,Henry,Python
02,Alice,C++
03,Smith,Science
在这里你可以找到上面的例子:
之前的答案涵盖了合并 csv。然而,您的问题是关于着色信息,到目前为止可能被忽略了,因为它没有意义。
如果您沉迷于着色 - 您需要不同于 csv 的格式。 csv 不包含任何格式信息:字体、颜色、列宽、行高等 none 这是普通 csv 的一部分。
假设我有一个 csv 文件并且我更改了一些行的颜色。现在我有一个从 Internet 下载的新 csv 文件。它有一些新行和一些旧行。我想在新文件中找到唯一数据并将其附加到旧文件,同时保留所有颜色更改。
我可以做其他所有事情,但颜色正在重置。这是我正在做的。
old_data = pd.read_csv('old_data.csv')
new_data = pd.read_csv('new_data.csv')
unique_data = new_data[~new_data['Booking Reference'].isin(old_data['Booking Reference'])]
unique_data.to_csv('old_data.csv', 'a')
这样做会重置颜色。有没有办法保留这些信息?
非常感谢任何帮助。
如果您希望将新行附加到 Python 中的 CSV 文件中,您可以使用以下任何一种方法。
将所需行的数据分配到列表中。然后,使用 writer.writerow() 将此列表的数据附加到 CSV 文件。 将所需行的数据分配到字典中。然后,使用 DictWriter.writerow().
将此字典的数据附加到 CSV 文件中我找到了一个符合您问题的示例。代码如下:
# Pre-requisite - Import the writer class from the csv module
from csv import writer
# The data assigned to the list
list_data=['03','Smith','Science']
# Pre-requisite - The CSV file should be manually closed before running this code.
# First, open the old CSV file in append mode, hence mentioned as 'a'
# Then, for the CSV file, create a file object
with open('CSVFILE.csv', 'a', newline='') as f_object:
# Pass the CSV file object to the writer() function
writer_object = writer(f_object)
# Result - a writer object
# Pass the data in the list as an argument into the writerow() function
writer_object.writerow(list_data)
# Close the file object
f_object.close()
在运行以上代码之前:
ID,NAME,SUBJECT
01,Henry,Python
02,Alice,C++
在运行上面的代码之后:
ID,NAME,SUBJECT
01,Henry,Python
02,Alice,C++
03,Smith,Science
在这里你可以找到上面的例子:
之前的答案涵盖了合并 csv。然而,您的问题是关于着色信息,到目前为止可能被忽略了,因为它没有意义。 如果您沉迷于着色 - 您需要不同于 csv 的格式。 csv 不包含任何格式信息:字体、颜色、列宽、行高等 none 这是普通 csv 的一部分。