跳到主要內容

發表文章

目前顯示的是 8月, 2020的文章

[python] 讀取csv to pandas(解決字串用雙引號出錯)

  def read_string_io_csv(file_name, headers):     with open(file_name, encoding="utf8") as csvfile:              csv_reader = csv.reader(csvfile, delimiter=',', quotechar='"', skipinitialspace=True, doublequote=False,                             quoting=csv.QUOTE_ALL, escapechar='\\')         line_count = 0         data = []         num_of_col = len(headers)         for row in csv_reader:                         data_row = {}             for col in range(num_of_col):                 data_row[headers[col]] = row[col]             data.append(data_row)             line_count += 1         return pd.DataFrame(data)