file-writing

Why is my Python "if" condition not working

Why is my Python "if" condition not working Question: I am writing a little utility that will read data from a pipe delimited file. If the 4th field in each row of the file is not empty, I want it write that row to another output file. I am attempting to loop through each row …

Total answers: 3

How to read a file in Python written out by C++

How to read a file in Python written out by C++ Question: I have one program written in C++ that outputs the data from several different types of arrays. For simplicity, I’m using ints and just writing them out one at a time to figure this out. I need to be able to read the …

Total answers: 1

How to write to a file in python without adding a new line except at certain place?

How to write to a file in python without adding a new line except at certain place? Question: I want to write to a file without adding a new line at the iterations of a for loop except the last one. Code: items = [‘1′,’2′,’3’] with open(‘file.txt’, "w") as f: f.write(‘test’ + ‘n’) for t …

Total answers: 3

Returning keywords found in a text file from a keyword list to a new file?

Returning keywords found in a text file from a keyword list to a new file? Question: Introduction: I’m currently building a keyword detection program. It is given a number of ‘.txt’ files and loops through them, searching for a keyword in them from a list of keywords, returning which files contained the keyword. The keywords …

Total answers: 3

Writing tensor to a file in a visually readable manner

Writing tensor to a file in a visually readable manner Question: In pytorch, I want to write a tensor to a file and visually read the file contents. For example, consider T = torch.tensor([3,4,5,6]). I want to write the tensor T to a file, say file_T.txt, and want to visually read the contents of the …

Total answers: 2

How to convert a PDF from base64 string to a file?

How to convert a PDF from base64 string to a file? Question: I have a PDF as a base64 string and I need to write it to file using Python. I tried this: import base64 base64String = “data:application/pdf;base64,JVBERi0xLjQKJeHp69MKMSAwIG9iago8PC9Qcm9kdWNlciAoU2tpYS9…” with open(‘temp.pdf’, ‘wb’) as theFile: theFile.write(base64.b64decode(base64String)) But it didn’t create a valid PDF file. What am I …

Total answers: 5

Python writelines() and write() huge time difference

Python writelines() and write() huge time difference Question: I was working on a script which reading a folder of files(each of size ranging from 20 MB to 100 MB), modifies some data in each line, and writes back to a copy of the file. with open(inputPath, ‘r+’) as myRead: my_list = myRead.readlines() new_my_list = clean_data(my_list) …

Total answers: 3

Write list variable to file

Write list variable to file Question: I have a .txt file of words I want to ‘clean’ of swear words, so I have written a program which checks each position, one-by-one, of the word list, and if the word appears anywhere within the list of censorable words, it removes it with var.remove(arg). It’s worked fine, …

Total answers: 2

How do I specify new lines on Python, when writing on files?

How do I specify new lines in a string in order to write multiple lines to a file? Question: How can I indicate a newline in a string in Python, so that I can write multiple lines to a text file? Asked By: FabianCook || Source Answers: The same way with ‘n’, though you’d probably …

Total answers: 16