readlines() not working when used on a CSV file - python

So have a "file.csv" and I want to extract some data from it using Python.
The problem for me is that the lines are formatted not in standard csv format, but instead a line looks like this:
;a_value=value;b_value=value;
So my idea was to use .replace() to edit everything in a line so that in the end it looks as wished:
'a_value':'value', 'b_value':'value'
In order to iterate and modify every line I followed the idea from this question from Modify csv files?
But I get:
AttributeError: 'str' object has no attribute 'readline'
My code:
with open(name_file, "r") as csv_file:
data=name_file.readlines()
csv_file=close()
csv_file=open(name_file, "w")
for row in csv_file:
#Replace commands
f.close()

It's something pretty simple in this case; When you open a file using with open, you need to operate on the file type variable you defined rather than using the path of the file.
In this case, you open the file but then try to read the string based path of the file, rather than the file type.
So you'd need to rewrite your code as such:
with open(name_file, "r") as csv_file:
data=csv_file.readlines()
Hope this helped!
P.S, the close method is only needed when you don't use the with keyword, when you exit the indented block, the file is closed.

Related

Python script not finding value in log file when the value is in the file

The code below is meant to find any xls or csv file used in a process. The .log file contains full paths with extensions and definitely contains multiple values with "xls" or "csv". However, Python can't find anything...Any idea? The weird thing is when I copy the content of the log file and paste it to another notepad file and save it as log, it works then...
infile=r"C:\Users\me\Desktop\test.log"
important=[]
keep_words=["xls","csv"]
with open(infile,'r') as f:
for line in f:
for word in keep_words:
if word in line:
important.append(line)
print(important)
I was able to figure it out...encoding issue...
with io.open(infile,encoding='utf16') as f:
You must change the line
for line in f:
to
for line in f.readlines():
You made the python search in the bytes opened file, not in his content, even in his lines (in a list, just like the readlines method);
I hope I was able to help (sorry about my bad English).

How to save multiple values to a file in python

So I would like to know how to save and check different values in a file. I have tried to read a file
with open(filename, 'r') as f:
varName = f.readline()
and this to write things to a file however this overwrites anything already in the file
with open('topname.txt', 'w') as f:
f.write(str(name))
f.close()
another problem is when I want to check the values by using a for loop however that didn't work. I was wondering if there was any other way to check, read and write values to/from a file.
An example of this is a login page where people can make accounts and log in and the program checks if the username and password exist.
When I looked on the internet it talked about a thing called pickle but it wasn't the same thing as what I wanted from what I could see.
For your first question, you need to open the file in append mode:
with open('topname.txt', 'a+') as f:
f.write(str(name))
f.close()
The '+' sign in append mode indicates that the script will create a new file if it doesn't already exist, if it does, it'll just append to it.
You want to "append" to the file, so open it with mode 'a' instead of 'w'.
Also, do not call f.close() inside of the with block. with will close the file for you after the block.
with open('topname.txt', 'a') as f:
f.write(str(name))
See: https://docs.python.org/3.6/library/functions.html#open
To append data to an existing file use open("Filename", "a") for "append mode." This is in contrast to your "read" and "write" modes.
if you want to read ,write and update, you need to open file file = open('topname.txt', 'a+') and to check every line to match user data for line in file:print(line) .Other operations like file.read(), file.write() will help. Remember to release resources by closing file finally file.close()

Python Tkinter save file as CSV creating extra spaces

Normally when I open a CSV file in Python, I need to use:
with open(filename, newline='', mode='w') as f:
And if I don't have that newline='' in there, it creates an empty line between each line in my CSV. However, I am using Tkinter to save the file, so I have:
new_filename = asksaveasfile(mode='w', defaultextension='.csv')
Since "new_filename" is already open, I can't do the "open" command to indicate the newline='' in there. If I try opening it again, I get an error. So how do I get rid of the extra spaces in this case?
Thanks for your help and patience.
you have some other problem regarding the new line parameter - I don't have to use it at all here. But for your tkinter problem, you can use asksaveasfilename instead. That returns the selected file name, then you can open it in any way you want.

File I/O in Python

I'm attempting to read a CSV file and then write the read CSV into another CSV file.
Here is my code so far:
import csv
with open ("mastertable.csv") as file:
for row in file:
print row
with open("table.csv", "w") as f:
f.write(file)
I eventually want to read a CSV file write to a new CSV with appended data.
I get this error when I try to run it.
Traceback (most recent call last):
File "readlines.py", line 8, in <module>
f.write(file)
TypeError: expected a character buffer object
From what I understood it seems that I have to close the file, but I thought with automatically closed it?
I'm not sure why I can write a string to text but I can't simply write a CSV to another CSV almost like just making a copy by iterating over it.
To read in a CSV and write to a different one, you might do something like this:
with open("table.csv", "w") as f:
with open ("mastertable.csv") as file:
for row in file:
f.write(row)
But I would only do that if the rows needed to be edited while transcribed. For the described use case, you can simply copy it with shutil before hand then opening it to append to it. This method will be much faster, not to mention far more readable.
The with operator will handle file closing for you, and will close the file when you leave that block of code (given by the indentation level)
It looks like you intend to make use of the Python csv module. The following should be a good starting point for what you are trying to acheive:
import csv
with open("mastertable.csv", "r") as file_input, open("table.csv", "wb") as file_output:
csv_input = csv.reader(file_input)
csv_output = csv.writer(file_output)
for cols in csv_input:
cols.append("more data")
csv_output.writerow(cols)
This will read mastertable.csv file in a line at a time as a list of columns. I append an extra column, and then write each line to table.csv.
Note, when you leave the scope of a with statement, the file is automatically closed.
The file variable is not really actual file data but it is a refernce pointer which is used to read data. When you do the following:
with open ("mastertable.csv") as file:
for row in file:
print row
file pointer get closed automatically. The write method expects a character buffer or a string as the input not a file pointer.
If you just want to copy data, you can do something like this:
data = ""
with open ("mastertable.csv","r") as file:
data = file.read()
with open ("table.csv","a") as file:
file.write(data)`

python clear csv file

how can I clear a complete csv file with python. Most forum entries that cover the issue of deleting row/columns basically say, write the stuff you want to keep into a new file. I need to completely clear a file - how can I do that?
Basically you want to truncate the file, this can be any file. In this case it's a csv file so:
filename = "filewithcontents.csv"
# opening the file with w+ mode truncates the file
f = open(filename, "w+")
f.close()
Your question is rather strange, but I'll interpret it literally. Clearing a file is not the same as deleting it.
You want to open a file object to the CSV file, and then truncate the file, bringing it to zero length.
f = open("filename.csv", "w")
f.truncate()
f.close()
If you want to delete it instead, that's just a os filesystem call:
import os
os.remove("filename.csv")
The Python csv module is only for reading and writing whole CSV files but not for manipulating them. If you need to filter data from file then you have to read it, create a new csv file and write the filtered rows back to new file.

Categories

Resources