How to append new rows to .txt file - python

Hi below is a piece of code from my command line interface on python i am trying to use this to create new users and append them to a .txt file, however whenever i append new data by running the programme it all saves to the rame row and i feel as though i have tried everything to solve this problem.
So please could anyone tell me how to append new data to a new row.
Thanks

That's because you've forgotten to add the new line character: \n.
with open('seano.txt', "a") as file:
file.write(str(user) + "\n")
See methods of file objects.

Related

Save a list to a .txt file and keep them

I found a way to print the list to a txt file, and it worked for me. Except for one detail,
with open("data.txt", "w") as output:
output.write(str(list))
When I make a different data entry, new data replaces the old data I entered, I want it not to delete the old data and continue recording new data.
using "w" in the open function will rewrite the complete file so use "a" in the open function which will append the file and you will not lose the original text in the file

Python - getline() method not printing "newlines"

I am relatively new to python and I am making a small game that involves importing each line of the text from a .txt file, so that it can be printed to the user. To do this, I'm using linecache.getline() to get the specific line of the file that I want while not having the whole file stored as a list. However, if I use "\n" to create a new line, then linecache.getline() automatically inserts another backslash to "cancel" it.
For example, if in the text file I write
\nHello,
linecache.getline() will store it in the variable as
\\nHello
which prints as
\nHello.
Is there any way to stop this? I can post my specific code if required.
Any help with file manipulation will be appreciated since I am very new to it and thank you for looking at my question.

How to select specific lines and re write it on a single file?

I have a .txt file which contains some data of the voltage, current and radiation lectures of an experiment, and it is displayed as it follows.
I am really new at Python, so I would like to know if there is any function or library that could help me to select only some lines, specifically those where the value displayed in the second row it is positive and higher than let's say, 10, and then re write it on a new file.
Also if you can recommend me any literature to get more into it, I would appreciate it. Thank you in advance!
You can use readlines()
with open('file_name','r') as f:
lines = f.readlines() #this will create a list with each element as a line
with open('file2','w') as f2:
f2.write(lines[0]) #this will write 1st line to a new file
f2.writelines(lines) #this will write all the lines in new file
Nothing is better than official documentation for learning,
If that isn't sufficient check it out

Excel CSV ending up all on one line

I'm looking for help in understanding why my name separator script isn't working. I am working through 'Automate the Boring Stuff With Python' and had the opportunity to test some things out at work today. I recognize this probably not the most efficient solution, but I'm trying to put my learning to work.
The Goal
I have an excel file with first and last names in a single cell. I need to separate these into two cells, one for first name and one for last name.
The Process
I began my saving the excel file as a .csv to then open in a text editor.
Used regular expressions to find the full name, grouping first and last names separately. (see the code in link provided)
I copy the raw .csv text to the clipboard using pyperclip (I don't know how to read from files yet.)
I extract the name data using the regex.
I run a for loop which creates a string with first name + ',' + last name + ',' so that excel will put the first and last names in different cells.
I want to end each firstName,lastName, pair with a new line so that my .csv file looks like:
firstname,lastname,
firstname2,lastname2,
etc...
I'm getting stuck on the last step. My for loop gets the firstname,lastname, pairs correct, but when I paste from the clipboard, the newline characters are not inserted. Everything is pasted as one huge string. Since I'm appending a new line character each cycle, shouldn't it paste everything on separate lines? Please help me understand what I'm missing!
Here is a link to my script: https://github.com/RNGeezus/name-separator/blob/master/name_separator.py
Here is what my .csv file looks like (recreated with dummy names to protect peopel's privacy):
my sample
Figured it out! Turns out I needed each pair to be followed by a \r\n. I was doing carriage returns, but no newlines. Doh!

The python command-line file handling doesn't work? am i working correctly?

I am a new python learner and now i have entered into file handling.
I tried solution for my problem but failed, so posting my question. before duplication please consider my question.
I tried to create a file, it worked.
writing in the file also worked.
But when i tried to read the text or values in the file, it returns empty.
I use command line terminal to work with python and running in Ubuntu OS.
The coding which I have tried is given below. The file is created in the desired location and the written text is also present.
f0=open("filehandling.txt","wb")
f0.write("my second attempt")
s=f0.read(10);
print s
I also tried with wb+, r+. But it just returns as empty
edit 1:
I have attached the coding below. I entered one by one in command line
fo = open("samp.txt", "wb")
fo.write( "Text is here\n");
fo.close()
fo = open("samp.txt", "r+")
str = fo.read(10);
print "Read String is : ", str
fo.close()
First of all if you open with wb flag then the file will be only in writeable mode. If you want to both read and write then you need wb+ flag. If you don't want the file to be truncated each time then you need rb+.
Now files are streams with pointers pointing at a certain location inside the file. If you write
f0.write("my second attempt")
then the pointer points at the [pointer before writing] (in your case the begining of the file, i.e. 0) plus [length of written bytes] (in your case 17, which is the end of the file). In order to read whole file you have to move that pointer back to the begining and then read:
f0.seek(0)
data = f0.read()

Categories

Resources