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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
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
NorieAnalyst Assistant Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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 BachPresident and Btrieve GuruCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.