我有一个超过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
您的行可能只是一个字符串,因此您可以使用:
根据您添加的代码示例,您试图回答的问题是如何为
pandas dataframe
中的每一行用', '
替换' '
。你知道吗有一种方法:
示例:
旧答案
您还可以使用
re.sub(..)
,如下所示:re.sub(...)
比str.replace(..)
快如果有多个空格分隔两个单词,那么基于
str.replace(' ',',')
的所有答案的输出都是错误的。例如要解决此问题,需要使用与一个或多个空格匹配的正则表达式,如下所示:
相关问题 更多 >
编程相关推荐