0

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
  • 23,210
  • 7
  • 49
  • 68
natalie22
  • 27
  • 4

2 Answers2

2

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
  • 155,494
  • 24
  • 217
  • 209
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,003
  • 2
  • 14