Posts: 16
Threads: 6
Joined: Mar 2017
Mar-07-2017, 02:24 PM
(This post was last modified: Mar-07-2017, 02:33 PM by Larz60+.)
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")
Posts: 11,900
Threads: 476
Joined: Sep 2016
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
Posts: 2,955
Threads: 48
Joined: Sep 2016
You can iterate over file lines
with open('file.txt', 'r') as file_obj:
for line in file_obj:
# do
Posts: 16
Threads: 6
Joined: Mar 2017
Mar-07-2017, 03:36 PM
(This post was last modified: Mar-07-2017, 03:36 PM by brocq_18.)
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()
Posts: 2,955
Threads: 48
Joined: Sep 2016
Consider that f.readlines() will create a list of all file lines. This can eat the memory. If the file is big enough.
Posts: 16
Threads: 6
Joined: Mar 2017
Mar-20-2017, 11:36 AM
(This post was last modified: Mar-20-2017, 11:36 AM by brocq_18.)
(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?
Posts: 2,955
Threads: 48
Joined: Sep 2016
Mar-20-2017, 12:00 PM
(This post was last modified: Mar-20-2017, 12:01 PM by wavic.)
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
Posts: 687
Threads: 37
Joined: Sep 2016
(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
Posts: 7,145
Threads: 122
Joined: Sep 2016
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
Posts: 16
Threads: 6
Joined: Mar 2017
Mar-21-2017, 11:55 AM
(This post was last modified: Mar-21-2017, 11:55 AM by brocq_18.)
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?
|