代码之家  ›  专栏  ›  技术社区  ›  Rivaldo Hater

在每个单词前加上Virgula

  •  2
  • Rivaldo Hater  · 技术社区  · 8 年前

    我有一个超过1000行的文本文件,在某个过程中,我需要用逗号分隔单词。我希望有人帮助我用python开发这个算法,因为我是从python语言开始的

    恩特拉达

    input phrase of the file to exemplify
    

    萨达

    input, phrase, of, the, file, to, exemplify
    

    import pandas as pd
    
     sampletxt = pd.read_csv('teste.csv' , header = None)
     output = sampletxt.replace(" ", ", ")
    
     print output
    
    5 回复  |  直到 8 年前
        1
  •  3
  •   pythomatic    8 年前
    the_list = entrada.split(" ") # take input & make a list of all values, separated by " "
    saida = the_list.join(", ") # join all elements with ", "
    
        2
  •  3
  •   Keith Cargill    8 年前

    您的行可能只是一个字符串,因此您可以使用:

    line.replace(" ",", ")
    
        3
  •  1
  •   Mohamed Ali JAMAOUI    8 年前

    根据您添加的代码示例,您试图回答的问题是如何替换 ' ' ', ' 对于a中的每一行 pandas dataframe .

    有一种方法可以做到这一点:

    import pandas as pd
    
    sampletxt = pd.read_csv('teste.csv' , header = None)
    output = sampletxt.replace('\s+', ', ', regex=True)
    print(output)
    

    例子:

    In [24]: l
    Out[24]: 
    ['input phrase of the file to exemplify',
     'input phrase of the file to exemplify 2',
     'input phrase of the file to exemplify 4']
    
    In [25]: sampletxt = pd.DataFrame(l)
    
    In [26]: sampletxt
    Out[26]: 
                                             0
    0    input phrase of the file to exemplify
    1  input phrase of the file to exemplify 2
    2  input phrase of the file to exemplify 4
    
    In [27]: output = sampletxt.replace('\s+', ', ', regex=True)
    
    In [28]: output 
    Out[28]: 
                                                    0
    0     input, phrase, of, the, file, to, exemplify
    1  input, phrase, of, the, file, to, exemplify, 2
    2  input, phrase, of, the, file, to, exemplify, 4
    

    旧答案

    您也可以使用 re.sub(..) ,如下所示:

    In [3]: import re
    
    In [4]: st = "input phrase of the file to exemplify"
    
    In [5]: re.sub(' ',', ', st)
    Out[5]: 'input, phrase, of, the, file, to, exemplify'
    

    re.sub(...) 快于 str.replace(..)

    In [6]: timeit re.sub(' ',', ', st)
    100000 loops, best of 3: 1.74 µs per loop
    
    In [7]: timeit st.replace(' ',', ')
    1000000 loops, best of 3: 257 ns per loop
    

    如果有多个空格分隔两个单词,则所有答案的输出基于 str.replace(' ',',') 将是错误的。例如

    In [15]: st
    Out[15]: 'input phrase of the file to  exemplify'
    
    In [16]: re.sub(' ',', ', st)
    Out[16]: 'input, phrase, of, the, file, to, , exemplify'
    
    In [17]: st.replace(' ',', ')
    Out[17]: 'input, phrase, of, the, file, to, , exemplify'
    

    In [22]: st
    Out[22]: 'input phrase of the file to  exemplify'
    
    In [23]: re.sub('\s+', ', ', st)
    Out[23]: 'input, phrase, of, the, file, to, exemplify'
    
        4
  •  1
  •   rachid el kedmiri    8 年前

    就复杂性而言,您应该直接用逗号替换空格,而不是多次遍历短语。

    the_list = entrada.replace(' ', ', ')
    
        5
  •  1
  •   peyo    8 年前

    首先,你需要 read your input on line at a time . 然后简单地使用str.replace():

    sampletxt = "input phrase of the file to exemplify"
    output = sampletxt.replace(" ", ", ")
    

    你就完了。