简体   繁体   中英

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?

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 = result.split(',')[0]

You can use split() to split the text where the comma is, then take the first piece:

with open('list.csv', "w") as csv_file:
    writer = csv.writer(csv_file)
    writer.writerows(results.split(',')[0])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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