将csv按行读取存入txt文件

第一版: 

import csv
file_path = "F:/zmz_experiment_data/dataset/train.csv"

with open(file_path,'r') as csvfile,open(file_path = "F:/zmz_experiment_data/dataset/train.txt") as f:
	reader = csv.reader(csvfile)
	for row in reader:
		print(row[0],row[1],file = f)

 简化版:

with open(file.csv, 'r') as f1,open(file.txt,'w') as f2:
    for line in f1:
        print(line.strip(),file = f2)

train.csv结构如下:

将csv按行读取存入txt文件_第1张图片

你可能感兴趣的:(Python,python)