python 3.5 appending .txt file not formatting correctly when opened in notepad - python-3.5

I am trying to append to a text file and write on a new line each time I append the file for readability in notepad. I believe this should be simple and researched thoroughly but I am still having an issue. Here is the snippet of code that writes to a .txt file:
appending_Text = data2
with open(file_Name, 'a+') as file:
file.write(appending_Text)
file.write('\n')
When I run this code and then check the text file, I get my appended data on the same line. When I open the .txt file using notepad, I want it to look like:
data1
data2
When I open the .txt file using notepad in windows, it looks like:
data1data2
What am I missing?

I figured out the answer and it's not python related but rather a limitation of notepad in windows. Notepad uses a different new line termination than is used in linux systems. Linux uses '\n' and notepad uses '/r/n'

Related

.txt file is opening but prints nothing

I'm trying to open a text file and print it as a string. I've made sure there is text in the .txt file but when I run the code it just prints an empty space, I don't know what to do at this point since I couldn't find anything that could help me with my problem.
with open('test.txt', 'r') as file:
data = file.read().rstrip()
print(data)
When things aren't opening check the following:
You wrote exactly the same in your code as the one you saved. "file.txt" is not the same as "File.txt" for Python (same goes for accents and special characters).
The file you are trying to read is in the same directory. If your code is at users/bla/documents/another_folder and you just pass the name of the file to your code, then the file must be at users/bla/documents/another_folder too. If not, be shure to add it into the string path as "path/to/your/file/file.txt"
Make sure that the extension .txt is the same as your file.
If you checked that but everything seems correct, try:
with open(path_to_file) as f:
contents = f.readlines()
And see if "contents" has something.
I think it is better if you use open("file.txt","r") function to do it. So your code will be like this:
file=open("test.txt","r")
data=file.read().strip()
print(data)

Open with Pandas in Python a .xls file that is corrupted

So here is the problem, I'm trying to import a DF from a file downloaded from COGNOS. In cognos I select .CSV format but always is downloaded the format is .xls
It will be very easy to open the .xls file and save as CVS but the problem with that is that the file has more rows than excel so I will lose a lot of data in the process. Also when I open the file in excel it is a warning that the file could be corrupted.
When I'm trying to open the data with df = pd.read_excel("Time Series 2018-1.xls") it shows the following problem.
XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\xff\xfeP\x00r\x00o\x00'
Please HELP
You can try
Change the file name, remove spaces and dash then try again
follow along with this pandas official link
I already resolve it. Just open the file in sublime and save with encoding UTF-8. Then just open it with df = pd.read_csv("Prueba1.xls", sep = "\t", encoding = 'utf8') because as #dougp said, is just a csv file save with the extension xls.
I guess there is a way to change the encoding in PYTHON but that's for another question.

Batch file command to open excel\csv as notepad

I can open an excel file from .bat by simply using the path of the file.
Is there a parameter to open the csv in edit mode (as notepad) ?
As Squashman mentioned in the comments, if you are just in the command prompt and need to edit any file, you can simply run notepad <filename>. However, if you need this to be part of a larger batch file or want it to be in batch form for another reason, you can use the below example which does a couple notable things:
Accepts the filename as a parameter.
Uses START so that the batch file will exit/resume immediately. If you leave this out, the batch file will wait for you to close notepad.
editfile.bat
#echo off
set "f=%1"
echo Opening %f% in notepad.exe
START notepad %f%
Usage: editfile filename.csv
Obviously, it doesn't have to be a CSV file. It can be any file.

Import data from text, read only

I have a Python script which continuously writes to a csv file.
I'd like to build an excel file which reads this csv file, and formats it nicely.
I tried to use "Data > From Text", but it turns out that, whenever this sheet then reads the csv file, it doesn't do this in a "read-only" way: the Python script becomes unable to open the csv file in write mode.
None of the dialogue boxes involved mention the possibility of using read-only mode.
Is there a way to accomplish this without resorting to VBA (which I'd really rather not touch)?

OpenText Hijacks the File Name in Powershell

A user suggested using the OpenText command to import a pipe delimited text file into excel. The problem is, that this seems to hijack the filename and forces it to be a .txt instead of an xlsx. When I try to
-replace ".txt.*", the .txt remains and when I try to force name the file .xlsx, it just makes a "corrupt" xlsx file that won't open properly in excel.
First Question
Finding Powershell names for Excel features
.txt and .xlsx are fundamentally different file types. You can't magically convert one into the other by just renaming the file.
Going out on a limb I guess your problem is that you're trying to save the file, and $excel.ActiveWorkbook.SaveAs($y) (or whatever statement you use) doesn't create an actual workbook.
Try saving it as xlWorkbookDefault:
$excel.ActiveWorkbook.SaveAs($y, 51)

Resources