[英]How do I remove everything after a certain character?
Code:代码:
with open('list.csv', "w") as csv_file:
writer = csv.writer(csv_file)
writer.writerows(results)
Example例子
If my output is 123,456789
, how would I remove everything after the comma?如果我的 output 是123,456789
,我将如何删除逗号后的所有内容?
you can just use the first element only (by slicing) when you write the rows.编写行时,您只能使用第一个元素(通过切片)。
Do something like this:做这样的事情:
with open('list.csv', "w") as csv_file:
writer = csv.writer(csv_file)
writer.writerows(map(lambda row:row[:1], results))
Supposing result
is your output, this will do:假设result
是您的 output,这将执行以下操作:
result = result.split(',')[0]
You can use split()
to split the text where the comma is, then take the first piece:您可以使用split()
拆分逗号所在的文本,然后取第一段:
with open('list.csv', "w") as csv_file:
writer = csv.writer(csv_file)
writer.writerows(results.split(',')[0])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.