在Python中将变量导出到 CSV 文件
CSV 文件或逗号分隔值文件用于跨平台存储和共享数据。列由逗号或其他相关分隔符分隔,使数据具有表格结构。
有时我们会遇到现成的 CSV 文件,有时我们需要根据项目的要求创建一个。 Python可以使用许多模块编写 CSV 文件。 Python 标准库中的 csv 模块提供了在 CSV 文件中读写数据的类和方法。
为了写入我们的 CSV 文件,我们使用 csv 模块的以下对象和方法:
writer() 方法
此函数返回一个 writer 对象,该对象将数据转换为分隔字符串并将其存储在文件对象中。该函数需要一个文件对象作为参数。为防止行之间增加空格,newline 参数设置为'' 。
编写器类具有以下方法:
- csv.writerow() – 此函数在可迭代对象(列表、元组或字符串)中写入项目,并用分隔符分隔它们
- csv.writerows() – 此函数将一个可迭代列表作为参数,并将它们中的每一个写入新行。
要写入 CSV 文件,让我们首先创建一个变量(列表、元组、字符串)。然后我们将在上述两个 csv 模块方法的帮助下将此变量导出到我们的文件中。
示例 1:将列表变量导出到 csv 文件。
Python3
import csv
# exporting a list variable into the csv file
input_variable = ['This', 'is', 'Geeks',
'For', 'Geeks','list']
# Example.csv gets created in the current working directory
with open('Example.csv', 'w', newline = '') as csvfile:
my_writer = csv.writer(csvfile, delimiter = ' ')
my_writer.writerow(input_variable)
Python3
import csv
# exporting a tuple variable into the csv file
input_variable = ('This', 'is', 'Geeks',
'For', 'Geeks', 'Tuple')
# Example.csv gets created in the current working directory
with open('Example.csv', 'w', newline = '') as csvfile:
my_writer = csv.writer(csvfile, delimiter = ' ')
my_writer.writerow(input_variable)
Python3
import csv
# exporting a string variable into the csv file
input_variable = "GeeksForGeeks"
# Example.csv gets created in the current working directory
with open('Example.csv', 'w', newline = '') as csvfile:
my_writer = csv.writer(csvfile, delimiter = ' ')
my_writer.writerow(input_variable)
Python3
import csv
# 2D list of variables (tabular data with rows and columns)
input_variable = [
['S.no','name','e-mail'],
[1,'meesha','meesh@email.com'],
(2,'abhilasha','ab@email.com'),
(3,'arav','arav123@email.com')
]
# Example.csv gets created in the current working directory
with open ('Example.csv','w',newline = '') as csvfile:
my_writer = csv.writer(csvfile, delimiter = ' ')
my_writer.writerows(input_variable)
输出:
示例 2:将元组变量导出到 csv 文件
蟒蛇3
import csv
# exporting a tuple variable into the csv file
input_variable = ('This', 'is', 'Geeks',
'For', 'Geeks', 'Tuple')
# Example.csv gets created in the current working directory
with open('Example.csv', 'w', newline = '') as csvfile:
my_writer = csv.writer(csvfile, delimiter = ' ')
my_writer.writerow(input_variable)
输出:
示例 3:将字符串变量导出到 CSV 文件
蟒蛇3
import csv
# exporting a string variable into the csv file
input_variable = "GeeksForGeeks"
# Example.csv gets created in the current working directory
with open('Example.csv', 'w', newline = '') as csvfile:
my_writer = csv.writer(csvfile, delimiter = ' ')
my_writer.writerow(input_variable)
输出:
同样,我们可以在二维列表的帮助下将多行写入 CSV。这种数据结构非常类似于具有行和列的表格 Excel 工作表。
例如:
下面的代码创建一个带有嵌套列表和元组列表的输入变量。
蟒蛇3
import csv
# 2D list of variables (tabular data with rows and columns)
input_variable = [
['S.no','name','e-mail'],
[1,'meesha','meesh@email.com'],
(2,'abhilasha','ab@email.com'),
(3,'arav','arav123@email.com')
]
# Example.csv gets created in the current working directory
with open ('Example.csv','w',newline = '') as csvfile:
my_writer = csv.writer(csvfile, delimiter = ' ')
my_writer.writerows(input_variable)
输出:
注意:成功执行上述代码后,CSV 文件中的更改将可见。每次运行后,您都需要检查 CSV 文件。