Write bold text to file [duplicate] - python

This question already has an answer here:
Plain-text formatting in python [closed]
(1 answer)
Closed 2 years ago.
i just want to write a string to a text file with part of this one in bold.
Is there any way to do it?
An example of what i am asking for:
2021/02/19: this is an example

You cannot use any type of markdown in a .txt file, using python or not.
You might want to check other files extensions such as .md files.

test = 'teste.rtf'
out_file = open(test,'w')
out_file.write("""{\\rtf1
This is \\b Bold \\b0\line\
}""")
out_file.close()

Related

Why does my first element in readlines() of a CSV have additional characters? [duplicate]

This question already has answers here:
Reading Unicode file data with BOM chars in Python
(7 answers)
Closed 4 years ago.
I ran the following python code to open a CSV, and the first element had some extra characters in it that aren't present when I view the CSV in a text editor, say Notepad++.
priorities_file = open('priorities.txt', 'r')
print('Name of the file: ', priorities_file.name)
p = priorities_file.readlines()
print('Read Line: %s' % (p))
The output looked like this:
Name of the file: priorities.txt
Read Line: ['Autonomy\n', 'Travel\n',...
I understand the '\n' and how to remove that from each element, but I don't understand why there are the additional characters in front of the element ' Autonomy'. Can anyone tell me why this is? Bonus points for a way to remove those characters which I honestly couldn't find how to reproduce.
repr() would help. (on Python 3.X; use ascii() instead).
p = priorities_file.readlines()
print(repr(p))
My hunch is that the ecnoding in the csv file is not actually ASCII or UTF8?
UPDATE:
This should do the trick:
p = p.decode("utf-8-sig")

How do I add text to a file without deleting what is already there? [duplicate]

This question already has answers here:
How do I append to a file?
(13 answers)
Closed 5 years ago.
I'm making a login system for my school task and when I try to add text to a Notepad file using Python it deletes what is already there so I can only store the information of one user at a time. Any idea how I can fix this? I've looked around on the internet but I can't find the right combination of words to get the search result I want... thanks in advance. I'm using IDLE (Python 3.2 GUI)
Edit: Problem solved, thanks for the help everyone.
You have to open file in append mode, for example:
f_obj = open('myfile','a+')
For that, you have to use "a" mode for appending data in a file.
f_obj = open("c:/file_path",'a')
f_obj.write("append")
or
with open("c:/file_path", "a") as f_obj:
f_obj.write("append")
You can use 'a for append' mode instead of 'r for right' mode, see following:
with open('your_exist_file.txt', 'a') as file:
file.write('appended_text')

How do I overwrite the text in a file using python3 so that the user can write more with out it interfering with old text? [duplicate]

This question already has answers here:
How to empty a file using Python
(2 answers)
Closed 6 years ago.
I'm confused and don't know what to do about this. I'm trying to overwrite the files text.
when opening a file with 'w' flag, it will rewrite the file if it exists.
with open('yourfile.ext', 'wt') as fileObj:
fileObj.write(stuff)

read a pdf in python [duplicate]

This question already has answers here:
How to read line by line in pdf file using PyPdf?
(3 answers)
Closed 7 years ago.
I want to read a pdf file in python. Tried some of the ways- PdfReader and pdfquery but not getting the result in string format. Want to have some of the content from that pdf file. is there any way to do that?
PDFminer is a tool for extracting information from PDF documents.
Does it matter in your case if file is pdf or not. If you just want to read your file as string, just open it as you would open a normal file.
E.g.-
with open('my_file.pdf') as file:
content = file.read()

Convert a PDF to text with Python [duplicate]

This question already has answers here:
How to extract text from a PDF file?
(33 answers)
Closed 2 months ago.
How can I get the content of pdf file line by line in python? I have searched in stackoverflow but could not find any good answer. Notes: pyPdf gives assertion erro, if possible something with slate and pdfminer.
from the command line:python /path/to/pdf2txt.py -o text.txt /path/to/yourpdf.pdf
You can then just take the text file it makes and use for line in file:
If you want to be efficient you would have to change pdf2txt.py, and have outfp be a python iostring, which would avoid the making a file and then reading from it.

Categories

Resources