Python - getline() method not printing "newlines" - python

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.

Related

How to import variables from another file AFTER the program has started in python?

I want to create a program that makes the user select a file, and that file will contain a series of variables (like dictionaries and lists) that the program will use. Now, I know there's the import file function but I can't use it because that's supposed to be at the beginning of the program (the user has to select which program wants to load first). The only step that's left in my program is the one that loads the variables, I know how to do the rest. Could anyone help me? Thank you in advance!
You could just use
with open(file, "r") as f:
data = f.read().splitlines()
to read the data, assuming every variable is on its own line. This will read in all variables in the file file as a list called data containing each file line as one entry.
Note that this will read all variables in as strings, so you may have to cast to different types before actually using the values.

Search for a word, and modify the whole line in Python text processing

This is my carDatabase.txt
CarID:c01 ModelName:honda VehicleType:city Price:20
CarID:c02 ModelName:honda VehicleType:x Price:30
I want to search for the carID and be only able to modify the whole line without interrupting others
my current code is here:
# Converting txt data into a string and modify
carsDatabaseFile = open('carsDatabase.txt', 'r')
allDataFromDatabase = [line.split(',') for line in carsDatabaseFile.readlines()]
Note:
Your question has a couple of issues: your sample from carDatabase.txt looks like it is tab-delimited, but your current code looks like it is splitting the line around the ',' character. This also looks like a place where a list comprehension might be hurting you more than it is helping you. Break that up into a for-loop if you're trying to add some logic to manipulate a single line.
For looking at CSV files, I would highly recommend using pandas for general manipulation of data in comma ceparated as well as a number of other formats.
That said, if you are truly restricted to only using built-in packages, or you are looking at this as a learning exercise, and your goal is to directly manipulate just one line of that file, what you are looking for is the seek method. You can use this in combination with the tell method ( documented just blow seek in the above link ) to find where you are in the file.
Write a for loop to identify which line in the file you are looking for
From there, you can get the output of tell() to find the specific place in the file you are trying to manipulate
Using the output from the above two steps, you can set the file pointer to a specific location using the seek() method (by byte: files are really stored as one dimensional).
You can now use the write() method to directly update the file at the location you determined above.

Python return to line above for writing

How can I make python program return to the start of output area after writing 4 lines of data. For example Program outputs fields 1....field 4 in different lines,after this program wants to add some data to line of field 1 ,but output is coming on line 5. The program is for converting data into tabular form.
If you are writing to a file, you can use the seek() function to relocate the file pointer wherever you want. For example, f.seek(0,0) will take you to the beginning of the file and then you can output the next data item there. However, keep in mind that you'll need to first move the data that you already wrote to the file, otherwise it will be over-written; that is, you need to "make space" for the new data you want to write to the beginning of the file.
For a quick intro, see https://docs.python.org/3.5/tutorial/inputoutput.html, near the bottom of the page.

How to append new rows to .txt file

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.

Splitting line on python

I need to use a python code without editing it (another's code).
At some point, this code reads the line of a text file to get some file names.
To do so, it uses a line.split()
On the example I was given, I had a file name like /home/directory/fileName
When I do a split on such a line, I get ['/home/directory/fileName\]
The point is the files I work on are located on "My Passport".
I had errors during the execution of the code that are caused by the name of the file.
Indeed, when I tried on python to split the following line: /media/My Passport/directory/fileName, I have ['/media/My', 'Passport/directory/fileName'], so a list with two elements, which the program I have cannot handle. This is because at some point of this code, fileName[0][0] is called, which should be ['/media/My 'Passport/directory/fileName'], but which is ['/media/My', 'Passport/directory/fileName']
I tried to change the name of my device, but it turns out I need to reformat it to do so... which I can't...
Anyone has an idea how I can handle this problem, specifically how I can modify the file names so that, after a line.split(), I get ['/media/My 'Passport/directory/fileName'] ??
Thank you
EDIT
I have a text file in which I have a list of file names with their path
/media/My Passport/fileName1
/media/My Passport/fileName2
/media/My Passport/fileName3
I have a code where I split the lines of this file line.split() to get lists like
['/media/My Passport/fileName1']
I know I can get such lists using line.split(\n), but I have to use line.split()
I am looking for a way to modify the text file so that, when I run line.split(), I get lists like
['/media/My Passport/fileName1']
and not
['/media/My', 'Passport/fileName1']
I have been trying to change the file text using brackets and backslashes :
"/media/My Passport/fileName1"
/media/My\ Passport/fileName1
but the same problem remains
Let us say you have
splitted_result = ['/media/My', 'Passport/fileName1']
Then you can do a simple join
>>> [' '.join(splitted_result)]
['/media/My Passport/fileName1']
This will output a list as its result.

Categories

Resources