Need help using index on a string in python - python

When I try to use indexing to get the last element of my line, it does not work( prints out a blank line). However, when I print the original line it works.
with open( newFile,"w") as f:
f.write("Model Number One" + "\n")
for l in lines:
if "ATOM" in l :
f.write(l[-1]) #Does NOT work, prints empty line
f.write(l) # Prints the whole linem when I only want the last element of the line
When index is used
When index is not used

The cause might be that the line has a new line character at the end. If the last element of the line is a "\n" since the file has multiple lines, then it will definitely print a blank line.

Each line is a string, and so each index is a single character, with the last one being a linebreak ('\n'). If you want to get whitespace-separated substrings out of that line, use strip to remove the linebreak, and split to split the resulting string on whitespace:
with open( newFile, "w") as f:
f.write("Model Number One" + "\n")
for l in lines:
elems = l.strip().split()
if "ATOM" in elems:
f.write(elems[-1] + "\n")

As Kengru mentioned, there could be a newline at the end. To test try f.write(l[-2]) instead of f.write(l[-1]).

Related

python doesn't append each line but skips some

I have a complete_list_of_records which has a length of 550
this list would look something like this:
Apples
Pears
Bananas
The issue is that when i use:
with open("recordedlines.txt", "a") as recorded_lines:
for i in complete_list_of_records:
recorded_lines.write(i)
the outcome of the file is 393 long and the structure someplaces looks like so
Apples
PearsBananas
Pineapples
I have tried with "w" instead of "a" append and manually inserted "\n" for each item in the list but this just creates blank spaces on every second row and still som rows have the same issue with dual lines in one.
Anyone who has encountered something similar?
From the comments seen so far, I think there are strings in the source list that contain newline characters in positions other than at the end. Also, it seems that some strings end with newline character(s) but not all.
I suggest replacing embedded newlines with some other character - e.g., underscore.
Therefore I suggest this:
with open("recordedlines.txt", "w") as recorded_lines:
for line in complete_list_of_records:
line = line.rstrip() # remove trailing whitespace
line = line.replace('\n', '_') # replace any embedded newlines with underscore
print(line, file=recorded_lines) # print function will add a newline
You could simply strip all whitespaces off in any case and then insert a newline per hand like so:
with open("recordedlines.txt", "a") as recorded_lines:
for i in complete_list_of_records:
recorded_lines.write(i.strip() + "\n")
you need to use
file.writelines(listOfRecords)
but the list values must have '\n'
f = open("demofile3.txt", "a")
li = ["See you soon!", "Over and out."]
li = [i+'\n' for i in li]
f.writelines(li)
f.close()
#open and read the file after the appending:
f = open("demofile3.txt", "r")
print(f.read())
output will be
See you soon!
Over and out.
you can also use for loop with write() having '\n' at each iteration
[Soln][1]
complete_list_of_records =['1.Apples','2.Pears','3.Bananas','4.Pineapples']
with open("recordedlines.txt", "w") as recorded_lines:
for i in complete_list_of_records:
recorded_lines.write(i+"\n")
I think it should work.
Make sure that, you write as a string.

Remove first character in line from text only if it matches defined character

I am receiving TCP data into a file. The data is meant for a POS printer. as such I need to strip control characters and other unwanted info. I have successfully stripped everything except the letter 'a' . However I only need to strip the character if it needed. Not every line will begin with the letter 'a'. Essentially I need to strip the letter 'a' from each line only if it is present as the first character. I don't need to strip every 'a' from the whole file.
Below is what I am doing but it is stripping every 'a' in the file.
unwanted_chars="[a]"
def Rema():
with open('Output.txt') as f:
lines=list(f)
for k, line in enumerate(lines):
for c in unwanted_chars:
line=line.replace(c,'')
lines[k]=line
with open('Output.txt','w') as f:
f.write('\n'.join(lines))
while True:
Rema()
.replace() iterates through an entire string and replaces all instances of the input with the new value given, so in this case, as you stated, all 'a's are being removed.
Strings can be called via indices just like lists in python so you could check if line[0] == 'a' and if so set the new line to be: line = line[1:]
Here is an example:
def Rema():
with open('Output.txt') as f:
lines=list(f)
for k, line in enumerate(lines):
for c in unwanted_chars:
if line[0] == c:
line = line[1:]
lines[k]=line
This is very specific to removing the first letter if it is 'a'. If you want to check for other letters AS the first letter only this will work for a longer list in unwanted_chars. But if you wanted to go back and remove all instances of say "\n" as an example in a string you would again use .replace()
If your printer doesn't like lines starting with an 'a' (for example), I'm guessing it's not going to like a line that started with 'aa' where you only remover the first 'a'.
How about using lstrip() for that:
def Rema():
with open('Output.txt') as f:
lines=(line.lstrip('a') for line in f)
with open('Output.txt','w') as f:
f.write('\n'.join(lines))
Below is the answer. Many thanks to Darren
def Rema():
with open('Output.txt') as f:
lines=list(f)
for k, line in enumerate(lines):
for c in unwanted_chars:
if line[0] == c:
line = line[1:]
lines[k]=line
with open('Output.txt','w') as f:
f.write('\r'.join(lines))

Python printing lines from a file

Salutations, I am trying to write a function that prints data from a text file line by line. The output needs to have the number of the line followed by a colon and a space. I came up with the following code;
def print_numbered_lines(filename):
"""Function to print numbered lines from a list"""
data = open(filename)
line_number = 1
for line in data:
print(str(line_number)+": "+line, end=' ')
line_number += 1
The issue is when I run this function using test text files I created, the first line is not on the same indentation level as the rest of the lines in the output, ie. the outputs look kind of like
1: 9874234,12.5,23.0,50.0
2: 7840231,70,60,85.4
3: 3845913,55.5,60.5,80.0
4: 3849511,20,60,50
Where am I going wrong? Thanks
Replace the value of end argument with empty string instead of space. As end argument is a space, it's printing a space after every line. So latter lines have a space at the beginning of the line.
def print_numbered_lines(filename):
"""Function to print numbered lines from a list"""
data = open(filename)
line_number = 1
for line in data:
print(str(line_number) + ": " + line, end='')
line_number += 1
Another way you can do this, is strip the new lines and print without passing any value to end argument. This will remove the \n it has at the end of the line and a new line will be printed as end="\n" by default.
def print_numbered_lines(filename):
"""Function to print numbered lines from a list"""
data = open(filename)
line_number = 1
for line in data:
print(str(line_number) + ": " + line.strip("\n"))
line_number += 1
This has to do with your print statement.
print(str(line_number)+": "+line, end=' ')
You probably saw that when printing your lines there was an extra line between them and then you tried to work around this by using end=' '.
If you want to remove the 'empty' lines you should use line.strip(). This removes them.
Use this:
print(str(line_number)+": "+line.strip())
strip can also take an argument. This is from the documentation:
str.strip([chars])
Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped:
Whats up with that?
The lines in your file are not separated into different lines by nothing. On linux a newline is represented by \n. Normal editors convert these by pushing the text down into a new line.
When reading a file Python separates lines on exactly these \n but doesn't throw them away. When printing they will be interpreted again and combined with the newline a print adds there will be one newline 'too much'.
The end parameter in your print statement simply changes what print will use after printing a line. Default is \n.
Check what it does when you use end=" !":
1: aaa
!2: bbb
!3: ccc
You can see the \n after 'aaa' causing a newline (which is part of the string) and after that print adds the contents of end. So it adds a !. The next line is printed in the same line because there is no other newline that would cause a line break before printing it.
You specified end argument as a space. So after first line each has this extra space.
line that your read from file looks somehting like this:
'9874234,12.5,23.0,50.0\n'
Look at the ending. Line translation happens is due to original line.
So to get what you want you just need to change end argument of print to empty string( not space)
Moreover, I advise you to change the implementation of the function and use enumerate for line numbering.
def print_numbered_lines(filename):
data = open(filename)
for i, line in enumerate(data):
print(str(i+1)+": "+line, end='')

python not writing when a space in text

I have working code which writes 'hi' in text file if 'Welcome' is present in the next line.
But, if the next line begins with whitespace before word 'Welcome' then it doesnot displays 'hi'
Code:
with open('afile.txt', 'r+') as f:
a = [x.rstrip() for x in f]
index = 0
for item in a:
if item.startswith("Welcome"):
a.insert(index, "hi")
break
index += 1
# Go to start of file and clear it
f.seek(0)
f.truncate()
# Write each line back
for line in a:
f.write(line + "\n")
Input: afile.txt
Welcome here
Good place
Expected output:
hi
Welcome here
Good place
I need to preserve my indendation also. How can I do that?
You are currently checking for Welcome directly. Instead, strip your line of whitespaces, and use the following condition instead
if item.strip().startswith("Welcome"):
EDIT
I see you've done rstrip earlier in a = [x.rstrip() for x in f]. Do a lstrip instead to remove whitespaces from the left. However, if you do this, your indentation will not be preserved.
In the line :
a = [x.rstrip() for x in f]
replace rstip with strip and you are good to go ...

CSV File to list

I have a CSV file which is made of words in the first column. (1 word per row)
I need to print a list of these words, i.e.
CSV File:
a
and
because
have
Output wanted:
"a","and","because","have"
I am using python and so far I have the follwing code;
text=open('/Users/jessieinchauspe/Dropbox/Smesh/TMT/zipf.csv')
text1 = ''.join(ch for ch in text)
for word in text1:
print '"' + word + '"' +','
This is returning:
"a",
"",
"a",
"n",
...
Whereas I need everything one one line, and not by character but by word.
Thank you for your help!
EDIT: this is a screenshot of the preview of the CSV file
Just loop over the file directly:
with open('/Users/jessieinchauspe/Dropbox/Smesh/TMT/zipf.csv') as text:
print ','.join('"{0}"'.format(word.strip()) for word in text)
The above code:
Loops over the file; this gives you a line (including the newline \n character).
Uses .strip() to remove whitespace around the word (including the newline).
Uses .format() to put the word in quotes ('word' becomes '"word"')
Uses ','.join() to join all quoted words together into one list with commas in between.
When you do :
text=open('/Users/jessieinchauspe/Dropbox/Smesh/TMT/zipf.csv')
that basically returns an iterator with each line as an element. If you want a list out of that and you're sure that there is only one word per line than all you need to do is
result=list(text)
print result
Otherwise you can get the first words only like so :
result = list(x.split(',')[0] for x in text)
print result
You could also use the CSV module:
import csv
input_f = '/Users/jessieinchauspe/Dropbox/Smesh/TMT/zipf.csv'
output_f = '/Users/jessieinchauspe/Dropbox/Smesh/TMT/output.csv'
with open(input_f, 'r') as input_handle, open(output_f, 'w') as output_handle:
writer = csv.writer(output_handle)
writer.writerow(list(input_handle))
If you put a comma at the end of the print statement it suppresses the newline.
print '"' + word + '"' +',',
Will give you the output on one line.
print ','.join('"%s"' % line.strip() for line in open('/tmp/test'))

Categories

Resources