简体   繁体   English

如何删除某个字符后的所有内容?

[英]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.

相关问题 如何删除一组字典中所有字典的字典中某个值后某个字符的所有内容? - How do I remove everything after a certain character in a value in a dictionary for all dictionaries in a group of dictionaries? 如何删除 Python 中某个字符之前的所有内容 - How to remove everything before certain character in Python (Python,熊猫)-如何将所有内容保留在某个角色的左侧? - (Python, Pandas) - How do I get everything to the left of a certain character? 如何删除字符串中某个字符后的所有内容? - How to delete everything after a certain character in a string? 如何在某个字符串后剥离所有内容 - how do I strip off everything after a certain string 如何删除df中整个列的某个字符后的所有内容? - How to delete everything after a certain character for whole column in df? Python 在特定字符之前删除字节串中的所有内容 - Python remove everything in bytestring before certain character 如何在Dataframe中最后一次出现字符后删除所有内容? - How to remove everything after the last occurence of a character in a Dataframe? 如何删除字符串中最后一个数字之后的所有内容(某些字符除外) - How to remove everything (except certain characters) after the last number in a string 如何在数据框列中的字符之后删除字符串的其余部分? - How do I remove the rest of a string after a character in a dataframe column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM