簡體   English   中英

如何在python中將字符添加到行首和行尾

[英]How to add a character to the beginning of a line and end of a line in python

我有一個從我的 IT 小組獲得的數據集。 他們有一個他們不願意改變的自動提取。

文件看起來像這樣(根據要求添加更多行)

col1#|#col2#|#col3#|#col4

數據1#|#數據2#|#數據3#|#數據4

數據1#|#數據2#|#數據3#|#數據4

cdata1#|#da#ta2#|#data3#|#data4

(第 4 行,這是一個示例,其中我的數據中的 # 搞砸了僅 # 的引號字符)

# 看起來他們試圖成為引號字符。 我想這樣使用它們,但無論出於何種原因,它們都沒有在每行的開頭或每行的末尾包含一個。 這些文件具有不同的列數,所以我試圖處理它們以基本上在每行的開頭和結尾添加一個 # 。

此外,由於 # 經常出現在我的數據中,我想將 # 轉換為 ### 以使導入到我的工具更干凈。

所以我想

###col1###|###col2###|###col3###|###col4###|###col4###

我怎么能做到這一點?

當前用於處理 csv 的代碼:

csv_pointer = open(file, encoding=CSV_Encoding, errors=Error_Detection)
csv_reader = csv.reader(
    csv_pointer,
    delimiter=CSV_Seperator,
    quoting=csv.QUOTE_NONE

)
batch = list()
# for each row in csv reader
for row in csv_reader:
    # append the processed row to the batch list
    # processed row meaning we strip down the fields to remove redundant 
data
    # and add Nones if the length of the row is not up to the FIELDS_COUNT
    batch.append([k.strip() for k in row] + [None] * (FIELDS_COUNT - len(row)))
# check if the batch length is greater than ROWS_AT_ONCE
if len(batch) >= ROWS_AT_ONCE:
    # if it is use the executemany over the cursor to insert the data in the batch list to the database
    curr.executemany(insert_func(Table_Name), batch)
    # commit
    conn.commit()
    # set the batch to empty list again
    batch = list()
# if the batch list is not empty
if batch:
# if it is use the executemany over the cursor to insert the data in the batch list to the database
curr.executemany(insert_func(Table_Name), batch)
# commit
conn.commit()
# delete batch (just incase the program message up and it need to delete the batch)
del batch

我試圖將我的分隔符更改為 #|#,這似乎可以解決我的問題,但它返回了錯誤:TypeError: "delimiter" must be a 1-character string

如果可以,為什么不使用您 IT 團隊的 csv 格式的分隔策略? 您可以在解析工具中拆分“#|#”(如果它在 python 中):

text="col1#|#col2#|#col3#|#col4"
values = text.split("#|#")
# values is ['col1', 'col2', 'col3', 'col4']

使用 csv 模塊,您必須指定quoting參數。 具體在第 2 行:

csv_reader = csv.reader(
    csv_pointer,
    delimiter=CSV_Seperator,
    quoting='#'
)

如果這干擾了您的字段中的'#' ,那么您可能需要對這個問題采取字面意義的方法(沒有 csv 庫):

batch = []
with open(file, r) as f:
     for l in f.readlines()[1:]: # if there's a header, if not, then remove the [1:]
         batch.append(l.split("#|#"))

這樣的事情對你有用嗎?

#Initial text
text="col1#|#col2#|#col3#|#col4"
#adds a ### to start and end
text='###{}###'.format(text)
#Replaces #|# with ###|### 
text=text.replace("#|#","###|###")

這將返回:

###col1###|###col2###|###col3###|###col4###

顯然,這需要某種形式的循環來遍歷您擁有的所有數據,也可以合並為一行,但我將其拆分以嘗試使其更清晰。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM