Python Forum
Need help amalgamating two scripts
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help amalgamating two scripts
#1
I have two scripts, the first which reads in a file and outputs a parsed version and then the second which takes the parsed version and parses it further.
How can I amalgamate these two to be a single script.

File 1:

with open("debt.txt", "r") as infile, open("outting.txt", "w") as outfile:
    data = infile.read()
    data = data.replace("£", "")
    outfile.write(data)
File 2:

with open("outting.txt", "r") as infile, open("out.txt", "w") as outfile:
    for line in infile:
        outfile.write(line.split()[1] + "\n")
Reply
#2
on your first script, use readlines() to read in line at a time
then add your spilt after the strip (which you can use to remove the pound symbol

see http://stackoverflow.com/questions/44831...o-its-sign for alternate conversion methods
Reply
#3
You can iterate over file lines


with open('file.txt', 'r') as file_obj:
    for line in file_obj:
        # do
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
Thank you both, didn't actually need an output file so this is what I have and is enough.

f = open('debt.txt','r')
for line in f.readlines():
    print line.split()[1].replace("£", "") + "\n"
f.close()
Reply
#5
Consider that f.readlines() will create a list of all file lines. This can eat the memory. If the file is big enough.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
(Mar-07-2017, 04:46 PM)wavic Wrote: Consider that f.readlines() will create a list of all file lines. This can eat the memory. If the file is big enough.

Changed this as follows;

with open('debt.txt') as d:
    for line in d:
        print line.split()[1].replace("£", "")

I am working in eclipse and the output gives me what I want but replaces the "£" with "�" instead; 

-�500
+�280
+�962
-�2088

When I add the line print sum(line.split()[1].replace("£", "")) I get an error: TypeError: unsupported operand type(s) for +: 'int' and 'str'.
How can I remedy this?
Reply
#7
You have a list with values ["-500", "+280", "+962", "-2088"] and whant to sum them?
In [1]: l = "balance: -£500 yes no perhaps?"

In [2]: print("{}".format(l.split()[1].replace("£", "")))
-500

In [3]: balance = ["-500", "+280", "+962", "-2088"]

In [4]: b = [int(v.strip('+')) for v in balance]

In [5]: b
Out[5]: [-500, 280, 962, -2088]

In [6]: sum(b)
Out[6]: -1346
str.strip(",.!@?") removes desired symbols from both sides of a string. Default are the blank symbols: \n, \r and space
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
(Mar-20-2017, 11:36 AM)brocq_18 Wrote:
(Mar-07-2017, 04:46 PM)wavic Wrote: Consider that f.readlines() will create a list of all file lines. This can eat the memory. If the file is big enough.

Changed this as follows;

with open('debt.txt') as d:
    for line in d:
        print line.split()[1].replace("£", "")

I am working in eclipse and the output gives me what I want but replaces the "£" with "�" instead; 

-�500
+�280
+�962
-�2088

When I add the line print sum(line.split()[1].replace("£", "")) I get an error: TypeError: unsupported operand type(s) for +: 'int' and 'str'.
How can I remedy this?

Smells like an character encoding problem... Is the file assumed to be encoded with UTF-8 or ISO-8859-15 or something else?
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#9
As mention it's a character encoding problem.
Keep it UTF-8 all the way,use io module for Python 2.x
Why are you not using Python 3.5-->?
Output:
uni.txt -£500 +£280
import io

with io.open('uni.txt', encoding='utf-8') as f:
   print(f.read())
Output:
-£500 +£280
Reply
#10
In Eclipse Neon I had #-*- coding: UTF-8 -*- at the top of the script and was still having those errors. Seems to be resolved now..Using Python 2.7 as it's what is currently on the machines at work.
My input and output is now;

In:

owe = []

with open('debt.txt', 'r') as a:
    for line in a:
        b = [line.split()[1].strip('+').replace("£", "") for line in a]
        print b
        
d = [float© for c in b]
sum(d)


Out: 

['-500', '280', '-2088', '-32.52']

-2340.52

But I have realised the answer is not correct as in b, the first value in debt.txt is being ignored for some reason, this value is -£13000. Does anyone have an explaination for this?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020