[英]Write .csv file from pandas dataframe with consecutive spaces as delimiter
I want to write a text file which is separated by four spaces instead of one tab: 我想写一个用四个空格而不是一个选项卡分隔的文本文件:
df.to_csv(file,sep= '\s\s\s\s')
instead of 代替
df.to_csv(file,sep= '\t')
I tried regex : 我尝试了regex:
df.to_csv(file,sep= r'\s{4}')
which did not work either ? 哪个也不起作用?
Is this doable without writing and the replacing the delimiter? 无需编写和替换定界符就可以做到吗?
This could be a workaround: 这可能是一种解决方法:
myCsv = df.astype(str).apply(lambda x: ' '.join(x), axis=1)
myCsv.rename(' '.join(df.columns)).to_csv(file, header=True, index=False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.