我有一個 csv 檔案,我正在嘗試用 python 清理它。
它有由 << \n >> 或空行分隔的行。
我希望將不以 << " >> 結尾的每一行剪切/粘貼到前一行。
這是一個更明確的具體示例!\
我擁有的 CSV 檔案
*"id","name","age","city","remark"\
"1","kevin","27","paris","This is too bad"\
"8","angel","18","london","Incredible !!!"\
"14","maria","33","madrid","i can't believe it."\
"16","john","28","new york","hey men,\
\nhow do you did this"\
"22","naima","35","istanbul","i'm sure it's false,\
\
\nit can't be real"
"35","marco","26","roma","you'r my hero!"\
"39","lili","37","tokyo","all you need to knows.\
\n\nthe best way to upgrade easely"\
...*
我想要的 CSV 檔案
*"id","name","age","city","remark"\
"1","kevin","27","paris","This is too bad"\
"8","angel","18","london","Incredible !!!"\
"14","maria","33","madrid","i can't believe it."\
"16","john","28","new york","hey men,how do you did this"\
"22","naima","35","istanbul","i'm sure it's false, it can't be real"\
"35","marco","26","roma","you'r my hero!"\
"39","lili","37","tokyo","all you need to knows. the best way to upgrade easely"\
...*
有人會怎么辦?
預先感謝您的幫助 !
我實際上正在嘗試這個python代碼-->
text = open("input.csv", "r", encoding='utf-8')
text = ''.join([i for i in text])
text = text.replace("\\n", "")
x = open("output.csv","w")
x.writelines(text)
x.close()
uj5u.com熱心網友回復:
for this_row in read_file.readlines():
if not this_row.startswith('"'):
prev_row = prev_row.rstrip('\n') this_row
else:
write_file.write(prev_row)
prev_row = this_row
只是一個草稿。您可以使用 str.join 和 list-cache 來獲得增強
uj5u.com熱心網友回復:
這里有幾點需要說明:
您的 CSV 檔案
,在您的備注中包含字符。這意味著該欄位必須用引號括起來(就是這樣)。CSV 檔案允許在單個欄位中包含換行符。這不會導致額外的資料行,但它確實使檔案對于人類來說很奇怪。
Python 的 CSV 閱讀器將自動處理欄位中的換行符。
最后,您的資料似乎被奇怪地編碼,您希望洗掉所有額外的換行符。每行還有一個不應存在的尾隨反斜杠字符。
我建議采用以下方法:
- 使用 Python 的 CSV 閱讀器一次正確讀取一行(您有 7 行 一個標題)。
- 從備注欄位中洗掉所有換行符。
例如:
import csv
with open('input.csv') as f_input, open('output.csv', 'w', newline='') as f_output:
csv_input = csv.reader(f_input)
csv_output = csv.writer(f_output)
for row in csv_input:
if len(row) == 5: # skip blank lines
row[4] = row[4].replace('\n', '').replace('\\n', ' ').replace('\\', '')
csv_output.writerow(row)
這會給你:
id,name,age,city,remark\
1,kevin,27,paris,This is too bad
8,angel,18,london,Incredible !!!
14,maria,33,madrid,i can't believe it.
16,john,28,new york,"hey men, how do you did this"
22,naima,35,istanbul,"i'm sure it's false, it can't be real"
35,marco,26,roma,you'r my hero!
39,lili,37,tokyo,all you need to knows. the best way to upgrade easely
uj5u.com熱心網友回復:
input.csv檔案內容:
"id","name","age","city","remark"
"1","kevin","27","paris","This is too bad"
"8","angel","18","london","Incredible !!!"
"14","maria","33","madrid","i can't believe it."
"16","john","28","new york","hey men,
how do you did this"
"22","naima","35","istanbul","i'm sure it's false,
nit can't be real"
"35","marco","26","roma","you'r my hero!"
"39","lili","37","tokyo","all you need to knows.
the best way to upgrade easely"
可能的(快速而簡單的)解決方案如下:
with open('input.csv', 'r', encoding='utf-8') as file:
data = file.read()
clean_data = data.replace('"\n"', '"||"').replace("\n", "").replace('"||"', '"\n"')
with open('output.csv', 'w', encoding='utf-8') as file:
file.write(clean_data)
回傳output.csv內容:
"id","name","age","city","remark"
"1","kevin","27","paris","This is too bad"
"8","angel","18","london","Incredible !!!"
"14","maria","33","madrid","i can't believe it."
"16","john","28","new york","hey men,how do you did this"
"22","naima","35","istanbul","i'm sure it's false,nit can't be real"
"35","marco","26","roma","you'r my hero!"
"39","lili","37","tokyo","all you need to knows.the best way to upgrade easely"
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/461483.html
