Unable to write in first line of csv file - python-3.x

I'm trying to append values to a csv file and convert it into a list. However, I'm not able to write on the first line of the csv file. Instead, the code starts writing from the second line. Any clarifications would be appreciated.
Thanks
time = get_time()
time_list = []
with open('time_data.csv', 'a', newline= '') as time_file:
time_file_write = csv.writer(time_file, delimiter=',',quotechar='|', quoting=csv.QUOTE_MINIMAL)
time_file_write.writerow([time])
with open('time_data.csv', 'r') as time_data:
read = csv.reader(time_data)
for i in read:
time_list.append(int(i[0]))

The error is not reproduceble. But...
A CRLF before the first line and no CRLF after the last line is what Windows CSV would have (older versions). New versions of Windows would keep the first line perfectly and add an additional CRLF after the last line. In Linux there will be no unwanted CRLF anywhere. Even if you re-define under write, etc, the basic behavior remains.
Few points to note:
The csv writerow takes different settings for Windows / Linux / etc as given under lib/csv.py. The settings are different for different versions of Windows. But it is the same for all flavors / versions of Linux. So, though the error does not show up in Windows here, you might have...
While asking...better if you furnish here a simple, to the point and very immediately executable code. Like...
import csv
time = '12'
with open('time_data.csv', 'a', newline= '') as time_file:
time_file_write = csv.writer(time_file, delimiter=',',quotechar='|', quoting=csv.QUOTE_MINIMAL)
time_file_write.writerow([time])
with open('time_data.csv', 'r') as time_data:
read = csv.reader(time_data)
for i in read:
print(i)
This code - after eliminating (1) the get_time() and (2) defining / populating the time_list - would make you understand where the issue is. And it ran as expected.
Considering the fact that this code (without get_time()) does not generate blank first line in Windows too and since the Code line time = get_time() adds ambiguity as we do not know what it returns to time, you need to check that too to resolve - if older Windows Version is not the case.

Related

Eliminate footer and header information from multiple text files (why can't I eliminate the last line as easily as I can eliminate the first lines?)

I have been trying all day.
# successfully writes the data from line 17 and next lines
# to new (temp) file named and saved in the os
import os
import glob
files = glob.glob('/Users/path/Documents/test/*.txt')
for myspec in files:
temp_filename = 'foo.temp.txt'
with open(myspec) as f:
for n in range(17):
f.readline()
with open(temp_filename, 'w') as w:
w.writelines(f)
os.remove(myspec)
os.rename(temp_filename, myspec)
# delete original file and rename the temp file so it replaces the original file
print("done")
The above works and it works well! I love it. I am very happy.
But this below does NOT work (same files, I am preprocessing files) :
# trying unsuccessfully to remove the last line which is line
# 2048 in all files and save again like above
import os
import glob
files = glob.glob('/Users/path/Documents/test/*.txt')
for myspec in files:
temp_filename = 'foo.temp.txt'
with open(myspec) as f:
for n in range(-1):
f.readline()
with open(temp_filename, 'w') as w:
w.writelines(f)
os.remove(myspec)
os.rename(temp_filename, myspec)
# delete original file and rename the temp file so it replaces the original file
print("done")
This does not work. It doesn't give an error, it prints done, but it does not change the file. I have tried range(-1), all the way up to range(-7), thinking maybe there were blank lines at the end I could not see. This is the only difference between the two blocks of code. If anyone could help that would be great.
To summarize, I got rid of permanently the headers and now I still have a 1 line footer I can not get rid of permanently.
Thank you so much for any help. I need to write permanently edited files. Because I have a ton of code that wants 2 or 3 column files without all the header footer junk, and the junk and file types vary widely. So if I lose the junk permanently ASCII can guess correctly the file types. And I really do not want to try and rewrite that code right now, it's very complicated and involves uncertainty and it took me months to get working correctly. I don't read the files until I'm inside a function and there are many files that are displayed in multiple drop downs. Thank you! All day I've been at this, I have tried other methods. I'd like to make THIS the above method work. To pop off the last and write it back to a permanent file. It doesn't like the -1. Right now it is just one specific line, it is (specifically line 2048 after the header is removed.) Therefore just removing line 2048 would be fine too. Its the last line of the files which are a batch of TSV files that are CCD readouts. Thanks in advance!

system is not writing into original dictionary file

def update(login_info):
stids = 001
file = open('regis.txt', 'r+')
for line in file:
if stids in line:
x = eval(line)
print(x)
c = input('what course you would like to update >> ')
get = x.get(c)
print('This is your current mark for the course', get)
mark = input('What is the new mark? >>')
g = mark.upper()
x.update({c: g})
file.write(str(x))
Before writing into the file
After writing into the file
This is what happens in the idle
As you can see, the system is not writing the data into the original dictionary. How can we improve on that? Pls, explain in detail. Thx all
Python doesn't just make relations like that. In Python's perspective, you are reading a regular text file, executing a command from the line read. That command creates an object which has no relationship to the line it was created from. But writing to the file should still work in my opinion. But you moved a line further (because you read the line where the data was and now you are at the end of it).
When you read a file, the position of where we are on the file changes. Iterating over the file like that (i.e for line in file:) invokes implicitly next() on the file. For efficiency reasons, positioning is disabled (file.tell() will not tell the current position). When you wrote to the file, for some reason you appended the text to the end, and if you test it it will no longer continue the loop even though it is still on the second line.
Reading and writing at the same time looks like an undefined behaviour.
Beginner Python: Reading and writing to the same file

file.read() not working as intended in string comparison

stackoverflow.
I've been trying to get the following code to create a .txt file, write some string on it and then print some message if said string was in the file. This is merely a study for a more complex project, but even given it's simplicity, it's still not working.
Code:
import io
file = open("C:\\Users\\...\\txt.txt", "w+") #"..." is the rest of the file destination
file.write('wololo')
if "wololo" in file.read():
print ("ok")
This function always skips the if as if there was no "wololo" inside the file, even though I've checked it all times and it was properly in there.
I'm not exactly sure what could be the problem, and I've spend a great deal of time searching everywhere for a solution, all to no avail. What could be wrong in this simple code?
Oh, and if I was to search for a string in a much bigger .txt file, would it still be wise to use file.read()?
Thanks!
When you write to your file, the cursor is moved to the end of your file. If you want to read the data aferwards, you'll have to move the cursor to the beginning of the file, such as:
file = open("txt.txt", "w+")
file.write('wololo')
file.seek(0)
if "wololo" in file.read():
print ("ok")
file.close() # Remember to close the file
If the file is big, you should consider to iterate over the file line by line instead. This would avoid that the entire file is stored in memory. Also consider using a context manager (the with keyword), so that you don't have to explicitly close the file yourself.
with open('bigdata.txt', 'rb') as ifile: # Use rb mode in Windows for reading
for line in ifile:
if 'wololo' in line:
print('OK')
else:
print('String not in file')

Checking/Writing lines to a .txt file using Python

I'm new both to this site and python, so go easy on me. Using Python 3.3
I'm making a hangman-esque game, and all is working bar one aspect. I want to check whether a string is in a .txt file, and if not, write it on a new line at the end of the .txt file. Currently, I can write to the text file on a new line, but if the string already exists, it still writes to the text file, my code is below:
Note that my text file has each string on a seperate line
write = 1
if over == 1:
print("I Win")
wordlibrary = file('allwords.txt')
for line in wordlibrary:
if trial in line:
write = 0
if write == 1:
with open("allwords.txt", "a") as text_file:
text_file.write("\n")
text_file.write(trial)
Is this really the indentation from your program?
As written above, in the first iteration of the loop on wordlibrary,
the trial is compared to the line, and since (from your symptoms) it is not contained in the first line, the program moves on to the next part of the loop: since write==1, it will append trial to the text_file.
cheers,
Amnon
You dont need to know the number of lines present in the file beforehand. Just use a file iterator. You can find the documentation here : http://docs.python.org/2/library/stdtypes.html#bltin-file-objects
Pay special attention to the readlines method.

.txt file is acting weird under readlines()

The contents of the file look like this:
1/15/13,930,1441.5
1/15/13,1000,1442.75
1/15/13,1030,1444
I run:
the_txt_file = open('/txt_file')
Then I run:
the_txt_file_as_a_list = the_txt_file.readlines()
Then I run:
print the_txt_file_as_a_list
And I get this:
['1/15/13,930,1441.5\r1/15/13,1000,1442.75\r1/15/13,1030,1444\r1/']
But I was expecting something like:
['1/15/13,930,1441.5\n','15/13,1000,1442.75\n','15/13,1030,1444\n']
This happens to me pretty frequently, what is going on?
So it seems that the problem had something to do with the way my mac interacted with the .txt file
The problem was fixed by swapping:
the_txt_file = open('/txt_file')
with:
the_txt_file = open('/txt_file', 'rU')
The 'rU' is called 'universal-readline'. Opening a file in 'rU' mode is opening a file in Universal readline mode. Upon running:
the_txt_file_as_a_list = the_txt_file.readlines()
and then:
print the_txt_file_as_a_list
my output went from:
['1/15/13,930,1441.5\r1/15/13,1000,1442.75\r1/15/13,1030,1444\r1/']
to:
['1/15/13,930,1441.5\n', '1/15/13,1000,1442.75\n', '1/15/13,1030,1444\n']
Later, I was able to print each item seperatly by:
for item in the_txt_file_as_a_list:
print item
The output looked like:
1/15/13,930,1441.5
1/15/13,1000,1442.75
1/15/13,1030,1444
I would assume that you, or the original creator of this data file were on a Mac. Seems you are expecting it to be a simple '\n' line ending, but suffer from the originating editors system default line ending (most likely).
An easy fix, is to call open(...) with the rU option like so:
the_txt_file = open('/txt_file', 'rU')
This ensures that the file is opened read only, and uses Universal newline support when reading the particular file.
Good luck!

Resources