Automate notepad++ editing csv file using script - python

So I have this code that generates a .csv file of data, however the formatting is off due to the escapechar (can't fix this). I need to make all the double spaces into single spaces. I can do this in notepad++ with replace all, so I've written a python script using a notepad++ plugin that does this. Now I'd like to automate opening the file and running the script; is this possible using a batch file? Is there a better way to do this?
Example of before and after format needed:
"_time","location"
"2018-04-03T08:32:45.565000-0400","(0 , 3)"
"2018-04-03T08:32:45.565000-0400","(2 , 5)"
"_time","location"
"2018-04-03T08:32:45.565000-0400","(0,3)"
"2018-04-03T08:32:45.565000-0400","(2,5)"

You can do it all with Python.
Just read the file and use the string replace method. Probably you will create a temporary file with the adjustments and then rename it. Something like:
with open(fname) as f:
lines = f.readlines()
for line in lines:
newline = line.replace(" ", " ") #two spaces become one space
#... write newline to temp file, etc.

Related

How to replace a particular character in a CSV file

I have a folder of CSV files (~100) and every file has an unknows character that looks like this �. This unknown character is supposed to be a double quote ("). Because of this unknown char, I am not able to run my CSV to xlsx converter to convert my files to XLSX format.
I tried using the csv.read() function but it does not with the replace function as csv.read() return a reader object and replace does not work with this. How can I replace that character and write the replaced contents back to csv so that I can run my csv to xlsx converter?
example :
current file contetnts:
"hello�
Output after convertion:
"hello"
Try this:
import fileinput
with fileinput.FileInput("file.csv", inplace=True) as file:
for line in file:
print(line.replace('�', '"'), end='')
The sed command is designed for this kind of work. It finds and replaces characters from a file.
Use this in a terminal.
sed -i 's/old-word/new-word/g' filename.csv
Your old-word should be the unknown character and new-word the double quote
I use this little function to deal with such problems.
The code is quite self-explanatory. It opens a file, read it all (may not work for files larger than your RAM) then rewrites it with a patched version.
def patch_file(file, original, patch):
with open(file, 'r') as f:
lines = f.readlines()
with open(file, 'w') as f:
for line in lines:
f.write(line.replace(original, patch))
patch_file(file='yourCSVfile.txt', original='�', patch'"')

python, when use writestr there is a text newline issue

I need your help~
I have a LF problem when I use 'wirtestr'.
the text have been written into zip well
but It comes in one line without line breaks.
Only I could find the delimiter which looks like square has circle in the middle of it, maybe hex code for newline.
If anyone knows about this problem please help!
fp = StringIO(line)
value = fp.getvalue()
filename1 = 'D:/re/m/11.txt'
filename2 = 'D:/re/m/dd.zip'
archive = zipfile.ZipFile(filename2, 'w', zipfile.ZIP_DEFLATED)
finfo = zipfile.ZipInfo(filename1)
archive.writestr(finfo, value)
The ZipFile.writestr method writes files from Python string in binary mode. All text files added with this method must then have explicit '\r\n' line endings for Windows programs to read them correctly afterwards.
Your original content had 'universal line ending' within python, which usually only turn into CRLF ('\r\n') when going through a text-mode output file.
That seems to be fixed in python 3.x

Printing a txt file inside python shell

I have an assignment for class that has me transfer txt data from excel and execute in python. But every time I run it, only hex is displayed. I was wondering how to have the data displayed in ascii in the shell. This is the code I have so far. Is it possible to print it out in ascii in the shell?
infile = open("data.txt", 'r')
listName = [line.rstrip() for line in infile]
print (listName)
infile.close()
The reason its not working is because you are opening an Excel file - which is in a special format and is not a plain text file.
You can test this by yourself by opening the file in a text editor like Notepad; and you'll see the contents aren't in text.
To open the file and read its contents in Python you will need to do one of these two things:
Open the file in Excel, then save it as a text file (or a comma separated file CSV). Keep in mind if you do this, then you can only save one sheet at a time.
Use a module like pyexcel which will allow you to read the Excel file correctly in Python.
Just opening the file as plain text (or changing its extension) doesn't convert it.

How to upload a .csv file changing the delimiter?

I'm using Python, and with the library gdata I can upload a .csv file, but the delimiter stays as default, that is comma (","). How can I do to change the delimiter to, for example, ";" ?
What I want is, from Python, change the delimiter of an uploading file. I don't want to change the "," to ";", I want to change the delimiter.
You could open the .csv using excel, then it knows its a csv (Comma Delimeted file) but you can set other things to 'delimit' the file by such as spaces etc.
Edit: Sorry, should've mentioned, don't open the file using the 'open with' method, open Excel first, then open the file from within Excel. This should open the 'Text Import Wizard' - Where you can choose what to delimt the file with such as tab,semicolon,comma,space etc.
I am assuming you really need to select this delimiter through gdata, right?
Otherwise you can easily change the delimiter in a shell with something like:
cat my_csv.csv | tr ',' ';' > my_csv_other_delimiter.csv
You can also easily replace these symbols in your python code. It could be an overload if you receive your csv files from somewhere else and you cannot control the symbol you use as a delimiter, but if there is no choice that could be an option.

Reading and writing files python

I am trying to write three separate line in a text document based on input obtained from a dialogue window. I am sure this is a simple fix but I can't seem to write the three lines as separate lines. Would someone mind telling me what's wrong with this bit of code?
file = open('file.txt', 'wb')
file.write('input1')
file.write('input2')
file.write('input3')
The inputs should be on different lines but instead they come out as:
input1input2input3
Instead of:
input1
input2
input3
Try this:
file = open('file.txt', 'wb')
file.write('input1\n')
file.write('input2\n')
file.write('input3\n')
You are appending the newline character '\n' to advance to the next line.
If you use the with construct, it will automatically close the file for you:
with open('file.txt', 'wb') as file:
file.write('input1\n')
file.write('input2\n')
file.write('input3\n')
Also, consider using a different variable name in place of file.
Your issue is that you haven't included newlines. Remember, Python is outputting like a typewriter--you don't tell it to go to a new line, it won't. The way to write a newline is \n.
So,
file.write('\n'.join([input1, input2, input3]))
Would do it.

Categories