是的,可以将字符串列表保存到CSV文件中,并且可以再将其读取为字符串列表。以下是一个Python示例代码:
import csv
# 保存字符串列表到CSV文件
def save_to_csv(strings, filename):
with open(filename, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(strings)
# 从CSV文件中读取字符串列表
def read_from_csv(filename):
with open(filename, 'r') as file:
reader = csv.reader(file)
strings = next(reader)
return strings
# 示例
strings = ['Hello', 'World', '123']
filename = 'example.csv'
# 保存字符串列表到CSV文件
save_to_csv(strings, filename)
# 从CSV文件中读取字符串列表
read_strings = read_from_csv(filename)
print(read_strings) # 输出: ['Hello', 'World', '123']
这个示例中,我们使用csv
模块来进行CSV文件的写入和读取。save_to_csv
函数将字符串列表写入到CSV文件中,read_from_csv
函数从CSV文件中读取字符串列表。最后,我们将读取到的字符串列表打印出来,结果为['Hello', 'World', '123']
。