从已解析的csv文件中删除逗号

时间:2019-11-07 08:13:12

标签: python python-3.x

我正在解析一个大型的csv文件,我需要知道如何从已解析的csv中删除逗号。 我正在将其解析为html,我想知道它在被解析的行上有一个特定字符,但是文档中充满了逗号,因此它没有模式,那么我该怎么做? 我尝试过

for line_number,line in enumerate(csv_file):
line.strip(",")
if line_number < 6:
    continue
print(line)


csv_row = line.split("\t") 
print(csv_row)

for col_num,entry in enumerate(csv_row):
#information always start on line 6
    if len(entry) < 1:
        continue
    if entry[0] and  entry[1] and entry[2]  == "~":
        html_text += "<div class = 'child_entries'>" + entry + "</div>"
    if entry_count > 0:
        html_text += "</div>"    
    #elif entry[1] == "~":
     #   print("aaaa")
      #  html_text += "<div class = 'child_entries'>" + entry + "</div>"

    #if last_child_entry_column < col_num:
     #   html_text += '<div class="child_entries">'    

    html_text += '<div class="entry">'
    #last_child_entry_column = col_num
    entry_count +=1 

html_text += '<div>'  + entry + '</div>'

并尝试使用re库并获得

AttributeError: 'str' object has no attribute 'write'

现在解析的文件看起来像这样

",,Description:a sequence of permission IDs from the backend global settings UserPermissions class.,,,,,,,,"

目标就是这样

"Description:a sequence of permission IDs from the backend global settings UserPermissions class"

那么,你们对我该怎么做有任何想法吗?

1 个答案:

答案 0 :(得分:0)

类“ str”的方法“ replace”不会在原地操纵字符串,而是返回被操纵的字符串。因此,您需要做的就是将该方法调用的结果保存到如下变量中:

line = line.replace(",", "")