0

I have a string as the result of a line.split from a file. How can I write this string to another file starting by the 5th element?

EDIT: I got this and it works:

for line in data.readlines ()
if not line.startswith ("H"):
s = line.split ()
finaldata.write (" ".join (s [5:]))
finaldata.write ("\n")

The only problem is that i have some empty "cells" in my input and that is messing the output (shifting the data to the left where i the original input has a blank)

How can i do it?

Thanks!

  • You know how to `split` the string, right? Do you know how to slice a list, like `lst[1:]` to get everything after the first element? From those two, you should be able to figure out the answer here. – abarnert Apr 25 '18 at 01:19
  • Find and replace _what_? – abarnert Apr 25 '18 at 01:20
  • 1
    Don't even need to split, just `print(s[s.index('e'):])` although you should use a numeric index if your letters repeat. – user3483203 Apr 25 '18 at 01:20
  • @chrisz That will only work if every one of his strings is `"a b c d e f g h"`—in which case it would be simpler to just `print("e f g h")`. – abarnert Apr 25 '18 at 01:24
  • I have a string s=line.split () , i don't know how to use slice to print s elements starting from the 4th one. I also want to find elements "A1" and "R1" and replace these elements with "P1" and "2" – Vandré Dreer Bonaite Apr 25 '18 at 01:29
  • If you know the element by count you should slice the string. string[5:] would print the 5th character to the end of the line. – sehafoc Apr 25 '18 at 01:30
  • But what is the syntax for slicing? – Vandré Dreer Bonaite Apr 25 '18 at 01:31
  • So the slicing has a pretty basic syntax; lets say you have a string a = "a b c d e f g h". you can slice "a" from the 5th character like this a[5:] outputs ' d e f g h'. Slicing syntax is [start:end:step] . so [5:] says start at 5 and include the rest. There are a ton of examples here https://stackoverflow.com/questions/509211/understanding-pythons-slice-notation – sehafoc Apr 25 '18 at 01:38

1 Answers1

0

To answer the original question: If you know the element by count you should slice the string. string[5:] would print the 5th character to the end of the line. Slicing has a pretty basic syntax; lets say you have a string

a = "a b c d e f g h" 

You can slice "a" from the 5th character like this

>>> a[5:] 
' d e f g h'

Slicing syntax is [start:end:step] . so [5:] says start at 5 and include the rest. There are a ton of examples here Understanding Python's slice notation

The second question isn't exactly clear what you're trying to achieve... Here are some examples of common standard string manipulations with inline comments

>>> a = "a b c d e f g h"
>>> a[5] # Access the 5th element of list using the string index
' '  
>>> a[5:] # Slice the string from the 5th element to the end
' d e f g h'  
>>> a[5::2] # Get every other character from the 5th element to the end
'     '
>>> a[6::2] # Get every other character from the 6th element to the end
'defgh'
# Use a list comprehension to remove all spaces from a string
>>> "".join([char for char in a if char != " "]) 
'abcdefgh'
# remove all spaces and print from the fifth character
>>> "".join([char for char in a if char != " "])[5:] 
'fgh'
>>> a.strip(" ") # Strip spaces from the beginning and end
'a b c d e f g h' 
>>> a[5:].strip(" ")  # slice and strip spaces from both sides
'd e f g h'
>>> a[5:].lstrip(" ")  # slice and use a left strip
'd e f g h'

Edit: To add in a comment from another user. if you know the character rather than the position, you can slice from that. Though, if you have duplicate characters you'll have to be careful.

>>> a[a.index("e"):] # Slice from the index of character
'e f g h'
>>> b = "a e b c d e f g h e"
>>> b[b.index("e"):]
'e b c d e f g h e'
sehafoc
  • 866
  • 6
  • 9