Python Forum
How to append and drop to next line while slice/indexing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to append and drop to next line while slice/indexing
#1
I'm practicing with opening, reading and appending to files. One of my goals was to open the file for the second time within the program and append a sliced segment from the original file. I was able to write the sliced segment, but I am lost on how to get it to drop to the next line. \n doesn't work in slicing. I have included the source code first and then divided the code with the output on the bottom.

with open("PythonSourceCode", "w") as python_file:
    content = python_file.write("with open(\"PythonSourceCode\", \"w\") as python_file:")

with open("PythonSourceCode", "a+") as My_Python_File:
    My_Python_File.seek(0)
    New_Content = My_Python_File.write("\nThis is practice")

with open("PythonSourceCode") as The_Final_Product:
    The_Final = The_Final_Product.read()

print(The_Final)

The_Final_Product.close()

with open("PythonSourceCode", "a+") as change:
    change.seek(0)
    new = change.read()
    newest = change.write(new[11:28])

with open("PythonSourceCode") as One_More:
    Last_One = One_More.read()

print(Last_One)
Output:
with open("PythonSourceCode", "w") as python_file: This is practice with open("PythonSourceCode", "w") as python_file: This is practicePythonSourceCode"
I'm running Ubuntu with python version 3.6
Reply
#2
Probably not the answer you are looking for, but automatic newline is added with print(). So one can do:

with open('sample.txt', 'w') as f:
    print('First row', file=f)

with open('sample.txt', 'a') as f:
    print('Second row', file=f)
Which results the sample.txt to have following content:

Output:
First row Second row
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Why not just write('\n') after writing the slice?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
just try
newest = change.write('\n'+new[11:28])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fix pandas copy/slice warning. deanhystad 3 814 Sep-07-2023, 03:18 PM
Last Post: deanhystad
  Slice creates new objects? fmr300 4 1,292 Jul-20-2022, 12:34 PM
Last Post: fmr300
  InvalidIndexError: (slice(None, None, None), slice(None, -1, None)) SuperNinja3I3 1 4,379 Jul-15-2022, 05:59 AM
Last Post: Larz60+
  [Solved] Delete a line in a dataframe using .reset_index() and .drop() ju21878436312 2 2,673 Feb-25-2022, 02:55 PM
Last Post: ju21878436312
  append a string to a modified line Mr_Blue 10 3,832 Sep-16-2021, 07:24 PM
Last Post: Mr_Blue
  Slice list Moris526 1 1,635 Dec-24-2020, 02:19 AM
Last Post: deanhystad
  increase and decrease a slice value? KEYS 2 2,082 Nov-10-2020, 11:35 PM
Last Post: KEYS
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 2,561 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  How to change 0 based indexing to 1 based indexing in python..?? Ruthra 2 4,310 Jan-22-2020, 05:13 PM
Last Post: Ruthra
  Pass Tuple as a Slice nagymusic 2 2,346 Dec-12-2019, 04:42 AM
Last Post: nagymusic

Forum Jump:

User Panel Messages

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