1

I'd like to remove everything after ? character in my csv.

Currently, it's like this: abc?123

I'd like to have it like this: abc

This is my code:

with open('Health.csv', 'r') as file, open('Health2.csv', 'w') as file2: 
    for line in file:
        line2 = line.rstrip('?')
        print(line2)
        file2.write(line2)

What am I doing wrong?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
natalie22
  • 33
  • 5

2 Answers2

3

rstrip() just removes that one character from the right-side, if present -- it will also remove multiple ?s... e.g. if the string was 'abc???' it will return 'abc'.

If ? is not the final or rightmost character or characters, rstrip() is not going to do anything.

Instead do:

line2 = line.split('?')[0] 

which splits on any ?, and the [0] just takes the part before the first ? if there are multiple. Example:

In [43]: 'a?b?c?'.split('?')[0]
Out[43]: 'a'
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
  • You could save all but the first split, you don't use them anyway: `line.split('?' ,1)[0] `. – Paul Jul 03 '23 at 16:39
1
with open('Health.csv', 'r') as file, open('Health2.csv', 'w') as file2: 
    for line in file:
        line2 = line.split('?')[0] 
        print(line2)
        file2.write(line2)
Erling Olsen
  • 1
  • 4
  • 15