標籤:ext tail cat writer fileread The run func pen
遇到的提示錯誤:_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?
錯誤code如下:
1 import csv 2 import sys 3 4 input_file = sys.argv[1] 5 output_file = sys.argv[2] 6 7 with open(input_file, ‘r‘) as csv_in_file: 8 with open(output_file, ‘w‘) as csv_out_file: 9 filereader = csv.reader(csv_in_file) 10 filewriter = csv.writer(csv_out_file)11 header = next(filereader)12 13 #print header14 15 filewriter.writerow(header)16 for row_list in filereader:17 #print row_list18 supplier = str(row_list[0]).strip()19 cost = str(row_list[3]).strip("$").replace(‘,‘, ‘‘)20 if supplier == ‘Supplier X‘ or float(cost)>600.0:21 filewriter.writerow(row_list)
解決方案:
將上述代碼第7行寫成 with open(input_file, ‘rU‘) as csv_in_file: 後解決
主要是開啟檔案的模式不正確。
‘U‘ universal newline mode (deprecated)——在python3中已經棄用
詳見:http://www.runoob.com/python/python-func-open.html
78808779
debug之csv檔案開啟報錯