CSV(逗号分隔值)文件是存储和共享表格数据的最常见方法之一。这些文件由行和列组成,其中每行代表一个记录,列包含由分隔符(通常是逗号)分隔的值。
Python 提供了多种内置方法来处理 CSV 文件,使您可以高效地读取、写入和操作数据。
本教程将为您提供有关使用 Python 处理 CSV 文件的深入知识。
使用 open() 方法读取 CSV
您可以使用open()
方法并以文本模式打开文件,然后迭代每一行,使用逗号作为分隔符分割值。
这是一个使用此的简单示例样本文件:
with open('sample.csv', 'r') as file:
for line in file:
row = line.strip().split(',')
print(row)
Output:
['"Index"', '"Name"', '"Age"', '"Favorite Color"']
['1', 'John', '6', 'Yellow']
['2', 'Ronald', '17', 'Red']
['3', 'Ruby', '23', 'Blue']
['4', 'Sarah', '53', 'Green']
['5', 'Timothy', '12', 'Green']
['6', 'Carol', '25', 'Red']
['7', 'Leo', '67', 'Blue']
['8', 'Quinn', '22', 'Green']
['9', 'Susan', '64', 'Orange']
['10', 'Luis', '79', 'Red']
在这里,open()
函数以读取模式(‘r’)打开文件‘sample.csv’,并且strip()
方法从每行中删除任何前导或尾随空格。
The split(',')
然后,方法用逗号分隔每一行,为每一行提供一个值列表。
读取特定行
您可以通过迭代 CSV 文件并选择所需的行索引来读取特定行。让我们读取第三行(索引 2)sample.csv
file.
import csv
def read_specific_row(row_number):
with open('sample.csv', 'r') as file:
reader = csv.reader(file)
for index, row in enumerate(reader):
if index == row_number:
print(row)
break
# Read the third row (index 2)
read_specific_row(2)
Output:
['2', 'Ronald', '17', 'Red']
阅读特定专栏
读取特定列涉及从每一行选择相同的索引。让我们从以下位置读取“名称”列(索引 1):sample.csv
file.
def read_specific_column(column_number):
with open('sample.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row[column_number])
# Read the "Name" column (index 1)
read_specific_column(1)
Output:
'Name'
'John'
'Ronald'
'Ruby'
'Sarah'
'Timothy'
'Carol'
'Leo'
'Quinn'
'Susan'
'Luis'
使用 Python csv.reader 读取 CSV
为了更结构化的 CSV 读取,Python 提供了专用的csv模块.
使用csv.reader
函数为您提供了更多控制权并使代码更具可读性。
这是一个例子:
import csv
with open('sample.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
Output:
['"Index"', '"Name"', '"Age"', '"Favorite Color"']
['1', 'John', '6', 'Yellow']
['2', 'Ronald', '17', 'Red']
['3', 'Ruby', '23', 'Blue']
['4', 'Sarah', '53', 'Green']
['5', 'Timothy', '12', 'Green']
['6', 'Carol', '25', 'Red']
['7', 'Leo', '67', 'Blue']
['8', 'Quinn', '22', 'Green']
['9', 'Susan', '64', 'Orange']
['10', 'Luis', '79', 'Red']
在这里,您导入csv
模块并使用csv.reader()
方法,传入打开的文件。
这将返回一个迭代器,允许您循环遍历行,就像使用open()
method.
The csv.reader
负责用逗号分隔行。
获取行数
您可以遍历行,并在行进行时对它们进行计数:
import csv
def get_number_of_rows(filename):
with open(filename, 'r') as file:
reader = csv.reader(file)
return sum(1 for row in reader)
rows_count = get_number_of_rows('sample.csv')
print("Number of rows (including the header):", rows_count)
Output:
Number of rows (including the header): 11
结果为您提供 CSV 文件中的行数,并且在计数中包括标题行。
处理不同的分隔符
如果您的 CSV 文件由逗号以外的任何分隔符分隔,您可以使用delimiter
范围。
以下是从此示例中读取以分号作为分隔符的文件的示例file:
import csv
with open('sample_semicolon.csv', 'r') as file:
reader = csv.reader(file, delimiter=';')
for row in reader:
print(row)
Output:
['"Index"', '"Name"', '"Age"', '"Favorite Color"']
['1', 'John', '6', 'Yellow']
['2', 'Ronald', '17', 'Red']
['3', 'Ruby', '23', 'Blue']
['4', 'Sarah', '53', 'Green']
['5', 'Timothy', '12', 'Green']
['6', 'Carol', '25', 'Red']
['7', 'Leo', '67', 'Blue']
['8', 'Quinn', '22', 'Green']
['9', 'Susan', '64', 'Orange']
['10', 'Luis', '79', 'Red']
通过提供delimiter
参数,您可以定义自定义字符来分隔值。
在本例中,分号“;”是分隔符,因此csv.reader
使用它将每一行分成几列。
使用 csv.DictReader 将 CSV 文件读入词典
With csv.DictReader
,CSV 文件中的每一行都作为 OrderedDict 返回,其中键是第一行中的字段名称。
操作方法如下:
import csv
with open('sample.csv', 'r') as file:
reader = csv.DictReader(file)
for row in reader:
print(row)
Output:
{'Index': '1', 'Name': 'John', 'Age': '6', 'Favorite Color': 'Yellow'}
{'Index': '2', 'Name': 'Ronald', 'Age': '17', 'Favorite Color': 'Red'}
{'Index': '3', 'Name': 'Ruby', 'Age': '23', 'Favorite Color': 'Blue'}
{'Index': '4', 'Name': 'Sarah', 'Age': '53', 'Favorite Color': 'Green'}
{'Index': '5', 'Name': 'Timothy', 'Age': '12', 'Favorite Color': 'Green'}
{'Index': '6', 'Name': 'Carol', 'Age': '25', 'Favorite Color': 'Red'}
{'Index': '7', 'Name': 'Leo', 'Age': '67', 'Favorite Color': 'Blue'}
{'Index': '8', 'Name': 'Quinn', 'Age': '22', 'Favorite Color': 'Green'}
{'Index': '9', 'Name': 'Susan', 'Age': '64', 'Favorite Color': 'Orange'}
{'Index': '10', 'Name': 'Luis', 'Age': '79', 'Favorite Color': 'Red'}
Here, csv.DictReader
读取 CSV 文件的第一行并将其用作字典的键。后续行将使用这些键读入字典,从而可以轻松按名称引用各个字段。
使用 open() 方法写入 CSV 文件
您可以使用open()
具有写入模式(‘w’)的方法并使用write()
method.
这是一个简单的例子:
data = [['Name', 'Age', 'Occupation'], ['John', '32', 'Engineer'], ['Jane', '28', 'Doctor']]
with open('output.csv', 'w') as file:
for row in data:
file.write(','.join(row) + '\n')
Output:
File output.csv
将有:
Name,Age,Occupation
John,32,Engineer
Jane,28,Doctor
在这里,open()
函数与“w”模式一起使用来打开文件进行写入。
在循环内部,','.join(row)
将值之间用逗号连接起来,并且write()
方法将每一行写入文件。
使用 csv.writer 写入 CSV 文件
编写 CSV 文件的一种更结构化的方法是使用csv.writer
方法从csv
module:
import csv
data = [['Name', 'Age', 'Occupation'], ['John', '32', 'Engineer'], ['Jane', '28', 'Doctor']]
with open('output.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(data)
Output:
File output.csv
将有:
Name,Age,Occupation
John,32,Engineer
Jane,28,Doctor
在这里,csv.writer()
方法提供了一种使用以下方法将行写入文件的简单方法writer.writerows()
功能。
The newline=''
参数确保在不同平台上正确处理行结尾。
使用 csv.DictWriter 编写字典
The csv
模块还提供了一种将字典写入 CSV 文件的便捷方法csv.DictWriter
。当数据已经是字典格式时,它特别有用。
使用方法如下:
import csv
data = [
{'Name': 'John', 'Age': '32', 'Occupation': 'Engineer'},
{'Name': 'Jane', 'Age': '28', 'Occupation': 'Doctor'}
]
with open('output.csv', 'w', newline='') as file:
writer = csv.DictWriter(file, fieldnames=['Name', 'Age', 'Occupation'])
writer.writeheader()
writer.writerows(data)
Output:
File output.csv
将有:
Name,Age,Occupation
John,32,Engineer
Jane,28,Doctor
With csv.DictWriter
,您提供fieldnames
作为确定列顺序的键列表。
The writeheader()
方法将键写入标题行,并且writer.writerows(data)
将字典作为行写入 CSV 文件中。
处理引号字符
当值包含逗号或换行符等特殊字符时,您可以使用引号。指定quotechar
and quoting
参数。
例子:
import csv
with open('quoted.csv', 'w', newline='') as file:
writer = csv.writer(file, quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(['Name', 'Age', 'Occupation, Title'])
Output:
File quoted.csv
将有:
Name,Age,"Occupation, Title"
通过设置quotechar
and quoting
,您可以控制特殊字符的处理方式。在上面的示例中,第三个值包含在引号中,因为它包含逗号。
操作列
添加列
向 CSV 文件添加新列可以通过读取文件、向每一行添加新列,然后将修改后的行写回新文件来完成。
这是一个例子:
import csv
# Adding a new column 'Salary'
with open('sample.csv', 'r') as infile, open('output.csv', 'w', newline='') as outfile:
reader = csv.reader(infile)
writer = csv.writer(outfile)
# Write headers with the new column
headers = next(reader)
headers.append('Salary')
writer.writerow(headers)
# Write rows with a new column value
for row in reader:
row.append('50000')
writer.writerow(row)
Output:
File output.csv
将有:
Index,Name,Age,Favorite Color,Salary
1,John,6,Yellow,50000
2,Ronald,17,Red,50000
3,Ruby,23,Blue,50000
4,Sarah,53,Green,50000
5,Timothy,12,Green,50000
6,Carol,25,Red,50000
7,Leo,67,Blue,50000
8,Quinn,22,Green,50000
9,Susan,64,Orange,50000
10,Luis,79,Red,50000
删除列
删除列可以通过在写入新文件时省略该列来实现。
例子:
import csv
with open('input.csv', 'r') as infile:
reader = csv.reader(infile)
with open('output.csv', 'w', newline='') as outfile:
writer = csv.writer(outfile)
for row in reader:
# Removing the Age column (index 2)
del row[2]
# Writing the modified row to the new file
writer.writerow(row)
Output:
File output.csv
将有:
Index,Name,Favorite Color
1,John,Yellow
2,Ronald,Red
3,Ruby,Blue
4,Sarah,Green
5,Timothy,Green
6,Carol,Red
7,Leo,Blue
8,Quinn,Green
9,Susan,Orange
10,Luis,Red
在此示例中,在写入新文件之前,将从标题和各个行中删除“年龄”列。
重命名列标题
以下是如何将“最喜欢的颜色”列重命名为“颜色”的示例sample.csv
file:
import csv
with open('sample.csv', 'r') as file:
reader = csv.reader(file)
rows = list(reader)
# Modify the header
rows[0][3] = 'Color'
# Write the data back to the file
with open('sample.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(rows)
Output:
"Index","Name","Age","Color"
1,John,6,Yellow
2,Ronald,17,Red
3,Ruby,23,Blue
4,Sarah,53,Green
5,Timothy,12,Green
6,Carol,25,Red
7,Leo,67,Blue
8,Quinn,22,Green
9,Susan,64,Orange
10,Luis,79,Red
操纵行
添加行
将行附加到 CSV 文件非常简单。您可以以追加模式(‘a’)打开文件并写入新行。
这是一个例子:
import csv
new_rows = [['11', 'Jake', '40', 'White'], ['12', 'Emily', '35', 'Black']]
with open('sample.csv', 'a', newline='') as file:
writer = csv.writer(file)
writer.writerows(new_rows)
Output:
File sample.csv
将有:
"Index","Name","Age","Favorite Color"
1,John,6,Yellow
2,Ronald,17,Red
3,Ruby,23,Blue
4,Sarah,53,Green
5,Timothy,12,Green
6,Carol,25,Red
7,Leo,67,Blue
8,Quinn,22,Green
9,Susan,64,Orange
10,Luis,79,Red
11,Jake,40,White
12,Emily,35,Black
在这里,以追加模式打开文件允许将新行添加到文件末尾,而不会覆盖现有内容。
删除行(过滤)
删除行需要读取文件,过滤掉不需要的行,然后将剩余的行写回文件。
例子:
import csv
with open('sample.csv', 'r') as infile, open('output.csv', 'w', newline='') as outfile:
reader = csv.reader(infile)
writer = csv.writer(outfile)
writer.writerow(next(reader))
# Write only the rows that have Age greater than 50
for row in reader:
if int(row[2]) > 50:
writer.writerow(row)
Output:
File output.csv
将有:
Index,Name,Age,Favorite Color
4,Sarah,53,Green
7,Leo,67,Blue
9,Susan,64,Orange
10,Luis,79,Red
此处,代码过滤掉“Age”小于 50 的行,并仅将剩余行写入新文件。
替换值
假设您想将年龄“17”替换为“18”。以下是实现这一目标的方法:
import csv
def replace_values(filename, old_value, new_value):
with open(filename, 'r') as file:
reader = csv.reader(file)
rows = [row for row in reader]
# Replace the old value with the new value
for row in rows:
for index, value in enumerate(row):
if value == old_value:
row[index] = new_value
# Write the updated data back to the file
with open(filename, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(rows)
replace_values('sample.csv', '17', '18')
Output:
"Index","Name","Age","Favorite Color"
1,John,6,Yellow
2,Ronald,18,Red
3,Ruby,23,Blue
4,Sarah,53,Green
5,Timothy,12,Green
6,Carol,25,Red
7,Leo,67,Blue
8,Quinn,22,Green
9,Susan,64,Orange
10,Luis,79,Red
如您所见,第二排的年龄是 17 岁,现在是 18 岁。
数据排序
您可以使用基于一列或多列对行进行排序sorted()
功能。
下面是按年龄排序的示例:
import csv
with open('sample.csv', 'r') as file:
reader = csv.reader(file)
rows = list(reader)
# Sort by Age (index 2)
rows[2:] = sorted(rows[2:], key=lambda row: int(row[2]))
with open('sorted.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(rows)
Output:
File sorted.csv
将有:
Index,Name,Age,Favorite Color
1,John,6,Yellow
5,Timothy,12,Green
2,Ronald,17,Red
8,Quinn,22,Green
3,Ruby,23,Blue
6,Carol,25,Red
12,Emily,35,Black
11,Jake,40,White
4,Sarah,53,Green
9,Susan,64,Orange
7,Leo,67,Blue
10,Luis,79,Red
在此示例中,代码将所有行读取到列表中,然后使用“年龄”列对行(不包括标题)进行排序sorted
函数和 lambda 函数作为键。然后将排序后的行写入新的 CSV 文件。
合并和连接 CSV 文件
当相关数据分布在多个文件中时,合并和连接 CSV 文件是一项常见任务。
您可以通过读取文件并根据公共键或列匹配行来实现此目的。
以下是在公共“ID”列上连接两个 CSV 文件的示例:
file1.csv
:
ID,Name
1,John
2,Jane
file2.csv
:
ID,Age
1,32
2,28
合并这些文件的代码:
import csv
file2_data = {}
with open('file2.csv', 'r') as file:
reader = csv.reader(file)
next(reader) # Skip header
for row in reader:
file2_data[row[0]] = row[1]
# Read file1.csv and join with file2.csv
with open('file1.csv', 'r') as infile, open('output.csv', 'w', newline='') as outfile:
reader = csv.reader(infile)
writer = csv.writer(outfile)
writer.writerow(next(reader) + ['Age'])
# Join and write rows
for row in reader:
writer.writerow(row + [file2_data[row[0]]])
Output:
File output.csv
将有:
ID,Name,Age
1,John,32
2,Jane,28
在这个例子中,file2.csv
首先使用“ID”列作为键读入字典。
Then, file1.csv
逐行读取,“年龄”列来自file2.csv
使用“ID”作为公共键附加到每一行。
处理缺失值
处理数据时,CSV 文件中的缺失值可能会导致错误或结果不准确。
您可以通过跳过缺失值、使用默认值填充缺失值或使用数据插补方法来处理缺失值。
下面是一个用默认值填充缺失值的示例:
import csv
with open('missing_values.csv', 'r') as infile, open('output.csv', 'w', newline='') as outfile:
reader = csv.reader(infile)
writer = csv.writer(outfile)
for row in reader:
# Replace missing values (empty strings) with 'N/A'
cleaned_row = [value if value != '' else 'N/A' for value in row]
writer.writerow(cleaned_row)
missing_values.csv
内容:
Name,Age,Occupation
John,,Engineer
Jane,28,
Output:
File output.csv
将有:
Name,Age,Occupation
John,N/A,Engineer
Jane,28,N/A
在此示例中,缺失值由 CSV 文件中的空字符串表示。该代码读取每一行并用“N/A”替换缺失值,然后将清理后的行写入新文件。
处理重复行
CSV 文件中的重复行可能会影响您的数据分析。您需要识别并删除重复项以确保工作的准确性。
以下是如何根据所有列删除重复行的示例:
import csv
seen_rows = set()
with open('duplicates.csv', 'r') as infile, open('output.csv', 'w', newline='') as outfile:
reader = csv.reader(infile)
writer = csv.writer(outfile)
for row in reader:
row_tuple = tuple(row)
if row_tuple not in seen_rows:
writer.writerow(row)
seen_rows.add(row_tuple)
duplicates.csv
内容:
Name,Age,Occupation
John,32,Engineer
Jane,28,Doctor
John,32,Engineer
Output:
File output.csv
将有:
Name,Age,Occupation
John,32,Engineer
Jane,28,Doctor
在此示例中,有一个名为seen_rows
跟踪已处理的行。如果遇到集合中已有的行,则会跳过该行,从而有效地从输出文件中删除重复项。
具有不同编码的 CSV 文件
文件可以使用不同的字符集进行编码,例如 UTF-8 或 ISO-8859-1。
读写不同编码的CSV文件需要在Python中指定编码。
下面是读取以 ISO-8859-1 编码的文件并以 UTF-8 写入的示例:
import csv
with open('input.csv', 'r', encoding='ISO-8859-1') as infile, open('output.csv', 'w', newline='', encoding='UTF-8') as outfile:
reader = csv.reader(infile)
writer = csv.writer(outfile)
for row in reader:
writer.writerow(row)
通过指定encoding
中的参数open
函数,可以处理不同编码的文件。
在此示例中,代码读取以 ISO-8859-1 编码的文件并使用 UTF-8 写入该文件。
在处理可能无法以标准 ASCII 编码表示的国际字符和符号时,这一点至关重要。
读取和写入大文件
当处理无法放入内存的大型 CSV 文件时,您可以逐行读取和写入文件以减少内存使用量。
以下是逐行读取和写入大文件的示例:
with open('large_input.csv', 'r') as infile, open('large_output.csv', 'w', newline='') as outfile:
for line in infile:
outfile.write(line)
此代码一次一行读取大型 CSV 文件,并将每一行写入一个新文件。通过避免将整个文件加载到内存中,它使您能够处理超出可用内存的文件。
虽然此方法内存效率高,但它可能并不适合所有数据处理任务,尤其是那些需要随机访问文件内容的任务。然而,对于简单的转换或过滤,它可能非常有用。
第三方库概述
虽然 Python 的内置 CSV 模块功能强大,但第三方库可以提供额外的功能和优化。
agate
Agate 是一个数据分析库,它扩展了 Python 内置 CSV 处理的功能。
import agate
table = agate.Table.from_csv('data.csv')
table.print_table()
Output:
| Index | Name | Age | Favorite Color |
| ----- | ------- | --- | -------------- |
| 1 | John | 6 | Yellow |
| 2 | Ronald | 17 | Red |
| 3 | Ruby | 23 | Blue |
| 4 | Sarah | 53 | Green |
| 5 | Timothy | 12 | Green |
| 6 | Carol | 25 | Red |
| 7 | Leo | 67 | Blue |
| 8 | Quinn | 22 | Green |
| 9 | Susan | 64 | Orange |
| 10 | Luis | 79 | Red |
Agate 提供 SQL 式查询、统计等功能。它对于复杂的数据分析任务特别有用。
基准阅读
使用 CSV 文件时,性能可能会因读取数据所用的方法而异。
我们来比较一下性能open()
, csv.reader
, csv.DictReader
, and pandas.read_csv
.
阅读更多关于如何使用 Pandas read_csv 读取 CSV 文件.
我们将使用一个样本文件有 1000 行和 10 列。
import csv
import pandas as pd
import timeit
# Using open()
def read_with_open():
with open('data.csv', 'r') as file:
return file.readlines()
# Using csv.reader
def read_with_csv_reader():
with open('data.csv', 'r') as file:
return list(csv.reader(file))
# Using csv.DictReader
def read_with_csv_dictreader():
with open('data.csv', 'r') as file:
return list(csv.DictReader(file))
# Using pandas read_csv
def read_with_pandas():
return pd.read_csv('data.csv')
print('open():', timeit.timeit(read_with_open, number=1000))
print('csv.reader:', timeit.timeit(read_with_csv_reader, number=1000))
print('csv.DictReader:', timeit.timeit(read_with_csv_dictreader, number=1000))
print('pandas read_csv:', timeit.timeit(read_with_pandas, number=1000))
Output:
open(): 1.220612900040578
csv.reader: 3.8335254000267014
csv.DictReader: 4.351687000016682
pandas read_csv: 4.525356799946167
open()
速度最快,但提供的功能最少。pandas.read_csv
提供最多的功能,但对于简单的任务可能会较慢。
基准写作
写入 CSV 文件也有不同的方法。我们来比较一下open()
, csv.writer
, csv.DictWriter
, and pandas.to_csv
.
阅读有关如何使用编写 CSV 文件的更多信息熊猫 to_csv.
我们将使用上述基准测试中使用的相同示例文件来写入其内容。
import csv
import pandas as pd
import timeit
# Read the data from sample.csv
with open('sample.csv', 'r') as file:
reader = csv.DictReader(file)
data = [row for row in reader]
# Using open()
def write_with_open():
with open('output.csv', 'w') as file:
for row in data:
file.write(','.join(map(str, row.values())) + '\n')
# Using csv.writer
def write_with_csv_writer():
with open('output.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(data[0].keys()) # Write headers
for row in data:
writer.writerow(row.values())
# Using csv.DictWriter
def write_with_csv_dictwriter():
with open('output.csv', 'w', newline='') as file:
writer = csv.DictWriter(file, fieldnames=data[0].keys())
writer.writeheader()
writer.writerows(data)
# Using pandas to_csv
def write_with_pandas():
df = pd.DataFrame(data)
df.to_csv('output.csv', index=False)
print('open():', timeit.timeit(write_with_open, number=1000))
print('csv.writer:', timeit.timeit(write_with_csv_writer, number=1000))
print('csv.DictWriter:', timeit.timeit(write_with_csv_dictwriter, number=1000))
print('pandas to_csv:', timeit.timeit(write_with_pandas, number=1000))
Output:
open(): 4.888178199995309
csv.writer: 6.31797530001495
csv.DictWriter: 8.040658099984284
pandas to_csv: 7.672513599973172
同样,这些时间安排反映了简单性和功能性之间的权衡。open()
速度更快,但需要手动处理,而pandas.to_csv
以牺牲性能为代价提供更多功能。
csv.reader 不工作(问题和修复)
文件未找到:确保 CSV 文件的名称和路径正确。如果 CSV 文件与 Python 脚本不在同一目录中,您将需要提供该文件的完整路径。
编码错误:如果您的 CSV 文件包含特殊字符,您可能会遇到编码问题。您可以在打开文件时指定编码:
with open('file.csv', 'r', encoding='utf-8') as file:
分隔符问题:如果您的 CSV 文件使用逗号以外的分隔符,则需要在创建时指定该分隔符csv.reader
object:
reader = csv.reader(file, delimiter=';')
换行问题:有时,处理换行符的方式可能会在读取 CSV 文件时引起问题。您可以添加newline=''
打开文件时的参数来处理此问题:
with open('file.csv', 'r', newline='') as file:
csv.writer 不工作(问题和修复)
文件权限错误:如果您尝试写入没有权限的位置,您将遇到错误。尝试写入您知道自己具有写入权限的位置。
缺少“换行符”参数:写入 CSV 文件时,通常建议包含newline=''
打开文件时。这有助于确保在不同平台上一致地处理换行符。
在“读取”模式文件中写入:确保您使用以下命令打开文件'w'
or 'a'
(写入或追加模式)而不是'r'
(阅读模式)。
数据结构不正确:确保您尝试写入的数据格式正确,通常是列表列表,其中每个内部列表代表 CSV 中的一行。
在其他地方打开 CSV 文件:如果您尝试写入的文件在另一个程序(例如 Excel)中打开,您可能会遇到问题。在写入文件之前,请确保该文件已在其他程序中关闭。
指定分隔符:如果您需要使用不同的分隔符(例如,用分号而不是逗号)编写 CSV 文件,您可以指定delimiter
范围:
writer = csv.writer(file, delimiter=';')
特殊字符编码:如果您要处理特殊字符,请确保使用适当的编码。您可以在打开文件时指定编码:
with open('file.csv', 'w', newline='', encoding='utf-8') as file: