Python Forum
Seperate entities in text file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Seperate entities in text file
#1
Hi all,
I am having problems writing seperate data entiries into a file. The code (listed below) keeps on appending the data into one long string, so I cannot read the data item back into a variable.
e.g if I enter 5 data values (1 [return] 2 [return] 3 [return] 4 [return] 5[return]} instead of writing the values as 1,2,3,4,5 into the file, it writes it as a string 1\n2\n3\n4\n5\n. So when I try to read the first value into a variable, I get 1\n2\n3\n4\n5\n instead of 1. I am trying to get the program to write and read back each number individually.
I have even tried opening and closing the file with each data input, hoping that this would give me individual entries in the file, but this did not work either
I hope this makes sense.

Thanks in advance

#Appending text to file

for i in range(5):
    new_file=open("test.txt",mode="a+",encoding="utf-8")
    er=input("Enter some text:")
    rt=er+"\n"
    new_file.write(rt)
    new_file.close()

#Reading data from test.txt
new_file=open("test.txt","r")


#for line in new_file:
for line in range(5):
    x=new_file.read()
    print(x)
new_file.close()
Reply
#2
A little more modern way,with with open() and f-string.
Can trow away close(),it's done automatically.
with open('test1.txt', 'w')  as f_out:
    for times in range(5):
        er = input("Enter some text: ")
        f_out.write(f'{er}\n')

with open('test1.txt') as f:
    numbers = [int(i.strip()) for i in f]
    print(numbers) # [1, 2, 3, 4, 5]
The first part dos the same as your code,so have 1\n2\n..ect
When i read it back strip of \n make integer list in one go(list comprehension).
Many times read(),readline() or readlines() is not needed,as can iterate over file object as i do with f.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,110 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  naming entities(city abbreviations) tirumalaramakrishna 1 1,235 May-06-2022, 11:22 AM
Last Post: jefsummers
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,649 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  How to seperate dict value? ilknurg 2 1,343 Mar-11-2022, 10:48 AM
Last Post: perfringo
  Converted Pipe Delimited text file to CSV file atomxkai 4 6,949 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  Two separate dataframes, two seperate programs stylingpat 2 1,993 Apr-28-2021, 07:56 PM
Last Post: stylingpat
  Seperate output buffer matt_the_hall 2 2,348 Mar-15-2021, 08:44 PM
Last Post: matt_the_hall
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,315 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  Saving text file with a click: valueerror i/o operation on closed file vizier87 5 4,390 Nov-16-2020, 07:56 AM
Last Post: Gribouillis
  saving data from text file to CSV file in python having delimiter as space K11 1 2,390 Sep-11-2020, 06:28 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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