我有一個包含 1 列的csv 檔案,其中每個單元格都有一個包含多個字串的串列。我創建了一個函式,它根據每個字串之前的符號(或不存在)添加 HTML 標記。當我嘗試將代碼插入到函式中以將其應用于每個單元格時,Python 會洗掉大部分字串(每個單元格僅保留一個)。相反,我想要與之前相同的字串,但添加了 HTML 標記。有人可以通過修復功能來幫助我嗎?謝謝!代碼:
import pandas as pd
df = pd.read_csv("input_file.csv")
def insert_HTML(row):
single_dash_prev_line = False
double_dash_prev_line = False
for line in row["text"]:
# print(line[0:2])
current_line = line
if line[0:1] != "$":
# print(line[0:1])
new_line = "</li></ul></b></strong><p>" current_line "</p>"
return new_line
elif line[0:1] == "$":
# print("got here first")
if line[0:2] != "$$":
# print(line[1:2])
if single_dash_prev_line == False:
new_line = "</b></strong><ul><li>" current_line[1:]
return new_line
single_dash_prev_line = True
elif single_dash_prev_line == True:
new_line = "</b></strong></li><li>" current_line[1:]
return new_line
single_dash_prev_line = True
elif line[0:2] == "$$":
# print("got here")
if single_dash_prev_line == True:
new_line = "</li></ul><b><strong>" line[2:]
return new_line
double_dash_prev_line = True
elif double_dash_prev_line == True:
new_line = "</b></strong></li></ul>" line[2:]
return new_line
double_dash_prev_line = True
elif single_dash_prev_line == True:
new_line = "</b></strong></li></ul>" current_line
return new_line
single_dash_prev_line = False
elif double_dash_prev_line == True:
new_line = "</b></strong></li></ul>" current_line[1:]
return new_line
single_dash_prev_line = False
df["new_text"] = df.apply(lambda row: insert_HTML(row), axis=1)
這是一個單元格的文本的樣子:
['$String1.',
'$String2.',
'$String3).',
'$String4.',
'String5.',
'$$String6.',
'String7.',
'']
這就是輸出的樣子(我知道 HTML 代碼不干凈;我將使用另一個工具來處理):
</b></strong><ul><li>String1.
</b></strong></li><li>String2.
</b></strong></li><li>String3.
</b></strong></li><li>String4.
</li></ul></b></strong><p>String5.</p>
</li></ul><b><strong>String6.
</li></ul></b></strong><p>String7.</p>
</li></ul></b></strong><p></p>
uj5u.com熱心網友回復:
而不是為串列中的每個專案“回傳新行”,只需繼續將中間結果附加到由新行分隔的字串中(結果 =新行 “\n”)。處理完串列中的所有專案后,回傳字串“result”。
這是代碼。
import pandas as pd
df = pd.read_csv("input_file.csv")
def insert_HTML(row):
single_dash_prev_line = False
double_dash_prev_line = False
result = ""
for line in row["text"]:
# print(line[0:2])
current_line = line
if line[0:1] != "$":
# print(line[0:1])
new_line = "</li></ul></b></strong><p>" current_line "</p>"
result = new_line "\n"
elif line[0:1] == "$":
# print("got here first")
if line[0:2] != "$$":
# print(line[1:2])
if single_dash_prev_line == False:
new_line = "</b></strong><ul><li>" current_line[1:]
result = new_line "\n"
single_dash_prev_line = True
elif single_dash_prev_line == True:
new_line = "</b></strong></li><li>" current_line[1:]
result = new_line "\n"
single_dash_prev_line = True
elif line[0:2] == "$$":
# print("got here")
if single_dash_prev_line == True:
new_line = "</li></ul><b><strong>" line[2:]
result = new_line "\n"
double_dash_prev_line = True
elif double_dash_prev_line == True:
new_line = "</b></strong></li></ul>" line[2:]
result = new_line "\n"
double_dash_prev_line = True
elif single_dash_prev_line == True:
new_line = "</b></strong></li></ul>" current_line
result = new_line "\n"
single_dash_prev_line = False
elif double_dash_prev_line == True:
new_line = "</b></strong></li></ul>" current_line[1:]
result = new_line "\n"
single_dash_prev_line = False
return result
df["new_text"] = df.apply(lambda row: insert_HTML(row), axis=1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/428816.html
