Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pandas read_excel()单元格中的换行符_Python_Excel_Pandas - Fatal编程技术网

Python Pandas read_excel()单元格中的换行符

Python Pandas read_excel()单元格中的换行符,python,excel,pandas,Python,Excel,Pandas,我使用python中的pandas将一张excel工作表提取到csv文件: import pandas as pd import sys read_file = pd.read_excel(sys.argv[1], engine='openpyxl', sheet_name = sys.argv[3]) read_file.to_csv (sys.argv[2], index = None, header=False, sep=';') 问题是单元格中的换行符(alt+enter

我使用python中的pandas将一张excel工作表提取到csv文件:

import pandas as pd 
import sys
    
read_file = pd.read_excel(sys.argv[1], engine='openpyxl', sheet_name = sys.argv[3])
read_file.to_csv (sys.argv[2],  index = None, header=False, sep=';')

问题是单元格中的换行符(alt+enter)将导致csv文件中出现新行。取而代之的是,它应该用一个空格分隔文本,mystring=mystring.replace('\n','').replace('\r','')将删除字符串的违规者,并且有一个匹配的函数

所以

你会得到你想要的


注意,windows使用回车符和换行符。一个被空间取代,另一个什么都没有。因此,上述替换操作之间存在差异。

mystring=mystring.replace('\n','').replace('\r','')
将删除字符串的违规者,并且有一个匹配的函数

所以

你会得到你想要的


注意,windows使用回车符和换行符。一个被空间取代,另一个什么都没有。因此,上述替换公式之间存在差异。

添加regex=True时,答案是正确的
read\u file=read\u file.replace('\n','',regex=True)
添加regex=True时,答案是正确的
read\u file=read\u file.replace('\n','',regex=True)
import pandas as pd 
import sys
    
read_file = pd.read_excel(sys.argv[1], engine='openpyxl', sheet_name = sys.argv[3])
read_file = read_file.replace('\n', ' ').replace('\r', '')
read_file.to_csv (sys.argv[2],  index = None, header=False, sep=';')