[已解決]excel import csv line break 問題

本帖最後由 bongbong3481 於 2023-9-30 21:21 編輯

用excel import csv file ,發覺某個Col 的field 內有line break,導致import csv file 後走晒位, 請問有咩方法解決到?

解決方法:最近學緊python,發覺用pandas 幾行code 搞好[將csv load 入pandas ,再save as xlsx 格式]

回復 1 #bongbong3481

多唔多? 小的話人手執咗先至import,

多的話, regen csv with another seperator

via HKEPC Reader for Android

TOP

回覆 2# fred2088


10幾萬行,個csv file 由系統generate 出黎,不能gen 另一種seperator, 只能在import 時候想方法。

TOP

TOP

kwliu 發表於 2023-5-7 21:59


公司機沒咩工具,只有notepad 及excel,  試左提及的方法,都系吾得。 google 左d code,    只能試下用vbs 將line break remove.

TOP

公司機沒咩工具,只有notepad 及excel,  試左提及的方法,都系吾得。 google 左d code,    只能試下用vbs ...
bongbong3481 發表於 2023-5-9 07:11



有冇dummy data放上黎試? 幾行就夠了

TOP

回覆 1# bongbong3481

Answered by ChatGPT:
  1. Sub RemoveLineBreaksFromCSV()

  2.     Dim strFile As String
  3.     Dim strText As String
  4.    
  5.     strFile = "C:\Users\username\Documents\example.csv" 'Change this to the file path of your CSV file
  6.    
  7.     'Read the contents of the CSV file
  8.     Open strFile For Input As #1
  9.     strText = Input$(LOF(1), 1)
  10.     Close #1
  11.    
  12.     'Replace line breaks with a space character
  13.     strText = Replace(strText, vbLf, " ")
  14.    
  15.     'Save the modified CSV file
  16.     Open strFile For Output As #1
  17.     Print #1, strText
  18.     Close #1
  19.    
  20.     'Import the modified CSV file into Excel
  21.     With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & strFile, Destination:=Range("A1"))
  22.         .TextFileParseType = xlDelimited
  23.         .TextFileCommaDelimiter = True 'Change this to the delimiter used in your CSV file
  24.         .Refresh
  25.     End With
  26.    
  27. End Sub

  28. This code first reads the contents of the CSV file into a string variable. It then replaces line breaks with a space character using the `Replace` function. The modified text is then saved back to the CSV file. Finally, the modified CSV file is imported into Excel using the `QueryTables.Add` method. You can customize this code to fit your specific needs, such as changing the file path and delimiter used in the CSV file.
複製代碼

TOP

回覆 7# lamsir

試左吾得, excel 出得一行row

TOP

回覆 6# jimking332


   d 資料是client 有關吾出得街,  吾知line break 是咩符號(只見到email 或電話分了2行),所以沒辦法人肉整一個sample 出黎

TOP

TOP