0

i made a code that generate a file like this

import sys
file = open('output.txt','w')
x = [1,2,3,4,5,6,7,8,9,10]
f = [i**2 for i in x]
g = [i**3/100 for i in x]

strlist = list(map(str,f))
strlist1 = list(map(str,g))

file.write(','.join(strlist))
file.write('\n')
file.write('.'.join(strlist1))

and result is (file content)

1,4,9,16,25,36,49,64,81,100
0.01.0.08.0.27.0.64.1.25.2.16.3.43.5.12.7.29.10.0

and i want to read this file as integer to make a list like this

[1,4,9,16,25,36,49,64,81,100]
[0.01.0.08.0.27.0.64.1.25.2.16.3.43.5.12.7.29.10.0]

i got this error when i tried

with open('output.txt','r') as a:
    data = a.readlines()[0]


with open('output.txt','r') as a:
    data1 = a.readlines()[1]

intlist = []
intlist.append(data)

['1,4,9,16,25,36,49,64,81,100\n']
['0.01.0.08.0.27.0.64.1.25.2.16.3.43.5.12.7.29.10.0']

how can i fix it?

Tibebes. M
  • 6,940
  • 5
  • 15
  • 36
To the moon
  • 31
  • 1
  • 6
  • What is your error? To help us solve it faster. – Dwa Oct 04 '20 at 06:26
  • The same way that you would do it if the string *didn't* come from a file. – Karl Knechtel Oct 04 '20 at 06:28
  • Does this answer your question? [How to read numbers from file in Python?](https://stackoverflow.com/questions/6583573/how-to-read-numbers-from-file-in-python) Note that this is space delimited, but the same logic apply here – David Oct 04 '20 at 06:45

2 Answers2

1

You made a whole process to make it a string delimited by ,s but haven't done the opposite way.

strlist = list(map(str,f))
strlist1 = list(map(str,g))

a = ','.join(strlist)
b = ','.join(strlist1)

strlist_as_int = [int(i) for i in a.split(',')]
strlist1_as_float = [float(i) for i in b.split(',')]

print(strlist_as_int)
print(strlist1_as_float)

output:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[0.01, 0.08, 0.27, 0.64, 1.25, 2.16, 3.43, 5.12, 7.29, 10.0]

In order to make it a complete whole answer, sometimes (in real cases rather than synthetic ones), the lines can contain non-integer, or non-float elements, in such case the best practice is to error-handle the exception like in here:

def convert_int_with_try_except(num):
    try:
        converted_int = int(num)
        return converted_int
    except ValueError:
        return num
        
def convert_float_with_try_except(num):
    try:
        converted_float = float(num)
        return converted_float
    except ValueError:
        return num
        
strlist = list(map(str,f)) + ["not_an_int"]
strlist1 = list(map(str,g)) + ["not_a_float"]

a = ','.join(strlist)
b = ','.join(strlist1)

strlist_as_int = [convert_int_with_try_except(i) for i in a.split(',')]
strlist1_as_float = [convert_float_with_try_except(i) for i in b.split(',')]

print(strlist_as_int)
print(strlist1_as_float)

output - pay attention that "non-integer" and "non-float" appended to list:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 'not_an_int']
[0.01, 0.08, 0.27, 0.64, 1.25, 2.16, 3.43, 5.12, 7.29, 10.0, 'not_a_float']
Yossi Levi
  • 1,258
  • 1
  • 4
  • 7
1

First when writing the strlist1, use ',' as a delimiter to avoid confusion whether the dot is indicating decimal point or not.

file.write(','.join(strlist))
file.write('\n')
file.write(','.join(strlist1)) # <- note the joining str

and when you read the file back,

with open('output.txt','r') as a:
    data = list(map(int, a.readline().split(','))) # split by the delimiter and convert to int
    data1 = list(map(float, a.readline().split(','))) # same thing but to a float

print(data)
print(data1)

output:

~ python script.py
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[0.01, 0.08, 0.27, 0.64, 1.25, 2.16, 3.43, 5.12, 7.29, 10.0]
Tibebes. M
  • 6,940
  • 5
  • 15
  • 36