简体   繁体   English

Python rstrip()不起作用

[英]Python rstrip() doesn't work

So I have a script ( Python: Openpyxl outputs "None" for empty cells ) that converts an XLSX file to a CSV file. 因此,我有一个脚本( Python:Openpyxl为空单元格输出“ None” ),该脚本将XLSX文件转换为CSV文件。

One of the cells is on multiple lines and contains no double quotes. 单元格之一在多行上,不包含双引号。 When I run rstrip() it still remains on multiple lines 当我运行rstrip()时,它仍然保留在多行中

Any ideas? 有任何想法吗?

for rownum in sh.iter_rows():
                values = [("" if cell.value is None else unicode(cell.value).encode('ascii','ignore').rstrip()) 
                    for cell in rownum]
                wr.writerow(values)

The first line in the CSV file is: CSV文件的第一行是:

"S. No","Summary","Question","Answer","Keywords","Product","Category","Access Level (Everyone, Help, Platinum)","Status 
Public (Customer facing)
Private (Internal only)"

How do I get the last cell 我如何获得最后一个细胞

rstrip() will only remove the whitespace at the end of the string not the newlines in the middle of the strings. rstrip()只会删除字符串末尾的空格,而不会删除字符串中间的换行符。 Do the following instead: lets say the variable values contains your string, then: 而是执行以下操作:假设变量值包含您的字符串,然后:

values = ' '.join(values.strip().split('\n'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM