'Error "fix_county_string(s) NameError: name 's' is not defined." I am trying to fix all counties in the file

Consider:

def fix_county_string(s):
    """ Insert Docstring """
    fp = open("michigan_COVID_08_24_21.txt", "r")
    fp.readline()
    for line in fp:
        county = line[24:43]
        x = county.split()
        t = x.pop(-1)
        s = x.append("County")
        return s
fix_county_string(s)

The parameter is s, a string. Every county name ends with the places; if it correctly ends in places, do nothing (simply return s). Otherwise, correct the ending word to be place. Specifically, if not, fix it.



Solution 1:[1]

Use:

def fix_county_string():
    """ Insert Docstring """
    fp = open("michigan_COVID_08_24_21.txt", "r")
    s = ''
    for line in fp:
        county = line[24:43]
        x = county.split()
        t = x.pop(-1)
        x.append("County")
        s += line + ' '.join(x)
    return s
s = fix_county_string()

I think this is what you are trying to do. You can write back the output in a file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Mortensen