在每个单词前面加上Virgula

2024-10-04 07:37:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个超过1000行的文本文件,对于某个特定的过程,我需要用逗号分隔单词。我希望有人能帮助我用python开发这个算法,因为我刚开始学习python语言

Entrada公司

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

Tags: ofcsvthetoinputoutput过程单词
3条回答
the_list = entrada.split(" ") # take input & make a list of all values, separated by " "
saida = the_list.join(", ") # join all elements with ", "

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

line.replace(" ",", ")

根据您添加的代码示例,您试图回答的问题是如何为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'

相关问题 更多 >