Reputation: 333
Any help is hugely appreciated. I'm trying to take a file that has a list of lines formatted like the sample below, remove the line break and any space at the end of the line and add a line number.
This:
9010
9011
9012
Needs to become this:
9010='0';9011=1';9012='2';
I've gotten pretty far but can't get it to remove the white spaces at the end of each number and can't get it to add the other characters. Here's my code:
f = open("datesfile.txt", "r")
lines = f.readlines()
lines =[each.replace('\n', '') for each in lines]
lines =[each.strip(' ') for each in lines]
f.close()
outtext = ["%s" + "=" + "\'" + "%s" + "\'" + ";" % (line,i) for i, line in enumerate(lines)]
outfile = open("variablerecord.txt","w")
outfile.writelines(str("".join(outtext)))
outfile.close()
But here's the error I get:
File "recodevariableforR.py", line 8, in <module>
outtext = ["%s" + "=" + "\'" + "%s" + "\'" + ";" % (line,i) for i, line in enumerate(lines)]
TypeError: not all arguments converted during string formatting
Why wouldn't all arguments convert? Is there a simpler way to do this?
Upvotes: 0
Views: 415
Reputation: 52161
The output line i.e. line 8
should be
outtext = ["%s = '%s';" % (line,i) for i, line in enumerate(lines)]
Upvotes: 2