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")
You can iterate over file lines
with open('file.txt', 'r') as file_obj:
for line in file_obj:
# do
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()
Consider that f.readlines() will create a list of all file lines. This can eat the memory. If the file is big enough.
(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?
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
(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?
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
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?