File I/O in Python - 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)`

Related

How to read the data from .sql file in python without using connection parameters

I have a .sql file, when i am trying to read data from .sql file using python open() function it's reading the file only in readlines() method. And removing the \n and spaces, I am trying to convert into .txt file which is not happening. I don't have connection params.
sql_file = open('SQL_SCript.sql', 'r')
lines = sql_file.readlines()
for line in lines:
line.strip()
with open('new_sql_script.txt', 'w') as new_sql_file:
new_sql_file.write(str(lines))
If all you need to do is change the extension, why no just copy to a new file with the right name. An SQL file is still a text file.
import shutil
shutil.copyfile('SQL_SCript.sql', 'new_sql_script.txt')
If you're determined to do it by reading and writing, it looks like what you're currently doing is overwriting the last line on every iteration.
You want something like this:
with open('SQL_SCript.sql') as in_file:
with open('new_sql_script', 'w') as out_file:
for line in in_file:
out_file.write(line)

readlines() not working when used on a CSV file

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.

Why isn't my csv file reading and writing correctly?

I have a csv file that consists of 3 elements per list. I'm trying to make the first element of each list a nested list of string elements that I can easily search through. I can't get my code to print out my results, and therefore I don't know if I've actually correctly rewritten the csv file.
with open('trump_tweets_proto.csv', 'w+') as file:
contents = csv.reader(file)
contents2 = csv.writer(file)
for row in contents:
for info in row:
contents2.writerow(row[0].split())
print(row[0])
You can't have a reader and writer object open on the same file at the same time. Or if you can, you certainly can't use both simultaneously and not mess up your file.
Open the file for reading (and be sure to look at the csv module documentation on how to open a csv file correctly, you're missing the newline parameter and probably should specify an encoding as well).
Open another (temporary) file for writing, and then copy the temp file over the old file. Or do everyting in memory with the first file, then open the file for writing and write the new data directly.

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.

Replace a word in a file

I am new to Python programming...
I have a .txt file....... It looks like..
0,Salary,14000
0,Bonus,5000
0,gift,6000
I want to to replace the first '0' value to '1' in each line. How can I do this? Any one can help me.... With sample code..
Thanks in advance.
Nimmyliji
I know that you're asking about Python, but forgive me for suggesting that perhaps a different tool is better for the job. :) It's a one-liner via sed:
sed 's/^0,/1,/' yourtextfile.txt > output.txt
This applies the regex /^0,/ (which matches any 0, that occurs at the beginning of a line) to each line and replaces the matched text with 1, instead. The output is directed into the file output.txt specified.
inFile = open("old.txt", "r")
outFile = open("new.txt", "w")
for line in inFile:
outFile.write(",".join(["1"] + (line.split(","))[1:]))
inFile.close()
outFile.close()
If you would like something more general, take a look to Python csv module. It contains utilities for processing comma-separated values (abbreviated as csv) in files. But it can work with arbitrary delimiter, not only comma. So as you sample is obviously a csv file, you can use it as follows:
import csv
reader = csv.reader(open("old.txt"))
writer = csv.writer(open("new.txt", "w"))
writer.writerows(["1"] + line[1:] for line in reader)
To overwrite original file with new one:
import os
os.remove("old.txt")
os.rename("new.txt", "old.txt")
I think that writing to new file and then renaming it is more fault-tolerant and less likely corrupt your data than direct overwriting of source file. Imagine, that your program raised an exception while source file was already read to memory and reopened for writing. So you would lose original data and your new data wouldn't be saved because of program crash. In my case, I only lose new data while preserving original.
o=open("output.txt","w")
for line in open("file"):
s=line.split(",")
s[0]="1"
o.write(','.join(s))
o.close()
Or you can use fileinput with in place edit
import fileinput
for line in fileinput.FileInput("file",inplace=1):
s=line.split(",")
s[0]="1"
print ','.join(s)
f = open(filepath,'r')
data = f.readlines()
f.close()
edited = []
for line in data:
edited.append( '1'+line[1:] )
f = open(filepath,'w')
f.writelines(edited)
f.flush()
f.close()
Or in Python 2.5+:
with open(filepath,'r') as f:
data = f.readlines()
with open(outfilepath, 'w') as f:
for line in data:
f.write( '1' + line[1:] )
This should do it. I wouldn't recommend it for a truly big file though ;-)
What is going on (ex 1):
1: Open the file in read mode
2,3: Read all the lines into a list (each line is a separate index) and close the file.
4,5,6: Iterate over the list constructing a new list where each line has the first character replaced by a 1. The line[1:] slices the string from index 1 onward. We concatenate the 1 with the truncated list.
7,8,9: Reopen the file in write mode, write the list to the file (overwrite), flush the buffer, and close the file handle.
In Ex. 2:
I use the with statement that lets the file handle closing itself, but do essentially the same thing.

Categories

Resources