converting python 2 to 3

Hi. I have been happily running an old python script but have just had to reinstall on new PC...so guessing I am now on Python3 rather than 2. My scripts fails at the point I strip the line.
THis is  a snippet of the python script that still works in an older version of geany:

input = open("hsbc.csv", "r")
lines = input.readlines()
input.close()
output = open("hsbc1.csv", "w")
result = []
for line in lines:
            line = re.sub('[,]', '', line)
            print >> output, line.strip()
output.close()

The error is noted at the 'print >> output.line.strip() line


Does anybody know how to convert the above script from Python2 to 3 so I can use it going forwards?
Thanks!... and you will guess I am no programmer!

I attach the file HSBC.csv
hsbc.csv
Richard TeasdaleFinancial ControllerAsked:
Who is Participating?
 
NorieVBA ExpertCommented:
I don't think strip() is the problem, I think it's the method you are trying to use to write to the output file.

For a start try changing this,
print >> output, line.strip()

Open in new window

to this.
output.write(line.strip())

Open in new window

0
 
NorieVBA ExpertCommented:
What's the error and what's the code meant to do?
0
 
Richard TeasdaleFinancial ControllerAuthor Commented:
OK. Screenshot of the error; and copies of file before and after (running under the old regime ).  THe file is a bank statement that I need to reconfigure to a csv file format that imports into a foxpro database. I had enough problems getting the original file into csv format, when the numbers have commas, and the field separators are semi colons! I think the step we are looking at takes out the commas, preparatory to replacing the semi colons with commas....the result file is the original file without any commas.
hsbcerror.png
hsbc1.csv
hsbc.csv
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Richard TeasdaleFinancial ControllerAuthor Commented:
Yay! Thank you very much. I have hit another problem but let me look at it first.
Thanks again!
0
 
peprCommented:
Note: It may be better to use the standard csv module for processing the csv file. You can configure it to do automatically what you are doing manually.
0
 
Bill BachPresidentCommented:
There is certainly nothing stopping you from installing Python 2.x on the computer, too.  If you have many such scripts, then this may be a much faster solution, and you can work on getting them onto Python 3 later on.
1
 
peprCommented:
To add to the Bill Bach's comment, the latest Python 2.7 is always a good option. Both versions can be installed side by side. And the Python 3 installs also the Python launcher for Windows, that will help you to launch easily both scripts for Python 2 and Python 3. From that point of view, you can just write new scripts in Python 3, and keep the old scripts in Python 2.
0
 
peprCommented:
I agree with Norie's that it is better to use output.write(...). However, when speaking about the rewriting Python 2 to Python 3, the error was caused by the print syntax. In Python 3, the print have been changed from the command to the built-in function. Therefore it has to have the arguments enclosed in parentheses. The special syntax with redirection via >> does not work any more. Anyway, there is direct equivalent to be used with the new print function. The original
print >> output, line.strip()

Open in new window

can be written as
print(line.strip(), file=output)

Open in new window

0
 
Richard TeasdaleFinancial ControllerAuthor Commented:
HI: I am so sorry but I thought I had accepted Norie's solution on the day.  I have now done so. PLease accept my apologies for delayed response! Also thanks again to all the other contributors.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.