Python Forum
item from a line to list however when i print the line instead of words i get letters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
item from a line to list however when i print the line instead of words i get letters
#1
Hey Everyone,

After some brainstorming and help from anbu23 i was able to make it work. However instead of words i get letters seperated like this . I tought of using rstrip on line however that didint solve the problem. I get results like this :

['A', 'd', 'r', 'e', 's', '\t', '\t', ':', ' ', 'S', 'U', 'L', 'T', 'A', 'N', ' ', 'M', 'U', 'R', 'A', 'T', ' ', 'M', 'A', 'H', '.', ' ', '2', '.', 'Å', 'ž', 'E', 'N', ' ', 'S', 'K', '.', ' ', 'N', 'O', ':', '4', ' ', 'Ä', '°', 'Ã', '‡', ' ', 'K', 'A', 'P', 'I', ' ', 'N', 'O', ':', '3', ' ', ' ', ' ', 'K', 'Ã', 'œ', 'Ã', '‡', 'Ã', 'œ', 'K', 'Ã', '‡', 'E', 'K', 'M', 'E', 'C', 'E', '/', 'Ä', '°', 'S', 'T', 'A', 'N', 'B', 'U', 'L']

From this code my aim as of now is getting info from a text file than find the columns that start with adres(or name) , than get each word from this line to a list.

Later also not included in this code i aim to paste this info to pre written word documents so when user gives raw info , program will find the reqired info like adres etc than paste word documents filled with info automatically saving lots of time for me. Any direction or suggestions for this is also vey appreciated.




fhand2=open("arb1.txt")
adressatırı=[]
for line2 in fhand2:
    satırsayısı2=satırsayısı2+1
    if satırsayısı2==isimsatırlistesi[0]:
        line2=line2.rstrip()#[b]thatdoesnt disappear the gaps also why does the list takes  letters instead[/b]
        for word in line2:
            adressatırı.append(word)
            satırdakikelimesayısı=len(adressatırı)
print(adressatırı[:satırdakikelimesayısı])
print(isimsatırlistesi[0])
print("isimlerin bulunduğu satırlar ",isimsatırlistesi)
print("toplam satırsayısı ise ",satırsayısı)
Reply
#2
Check the spelling of your python (/phyton) tags. The editor has buttons for making tags.
Reply
#3
(Apr-22-2020, 01:24 PM)deanhystad Wrote: Check the spelling of your python (/phyton) tags. The editor has buttons for making tags.
Thanks didnt know about the buttons :)
Reply
#4
if satırsayısı2==isimsatırlistesi[0]:
        line2=line2.rstrip()
        for word in line2.split():
            adressatırı.append(word)
            satırdakikelimesayısı=len(adressatırı)
Reply
#5
Looks like i found the solution, using "strip" instead of "split".

 line2=line2.rstrip()
this should be,

 line2=line2.rsplit()
.

I can still take input about creating word document inside python tough :).

(Apr-22-2020, 02:03 PM)anbu23 Wrote:
if satırsayısı2==isimsatırlistesi[0]:
        line2=line2.rstrip()
        for word in line2.split():
            adressatırı.append(word)
            satırdakikelimesayısı=len(adressatırı)

Hah ok looks like you beat me :).
Reply
#6
I may be really missing something, but isn't this all you need?
fhand=open("arb1.txt")

adressatiri=[]
for line in fhand:
    if line.startswith("Adres"):
        adressatiri += line.split()
fhand.close()

print(adressatiri)
This program finds lines that start with "Adres", splits the line into words, and appends the words to the list adressatiri.

Are you expecting multiple lines to "Adres"? If so do you want all the words in one list, or do you want each a list of words for each matching line? If the second, replace "adressatiri += line.split()" with "adressatiri.append(line.split())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  count item in list korenron 7 283 Jul-01-2021, 01:46 PM
Last Post: deanhystad
  How to capture string from a line to certain line jerald 1 228 Jun-30-2021, 05:13 PM
Last Post: Larz60+
  [Solved] Reading every nth line into a column from txt file Laplace12 7 441 Jun-29-2021, 09:17 AM
Last Post: Laplace12
  [solved] unexpected character after line continuation character paul18fr 4 306 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  Reading and storing a line of output from pexpect child eagerissac 0 165 Jun-17-2021, 12:22 PM
Last Post: eagerissac
  pandas.errors.ParserError: Error tokenizing data. C error: Expected 9 fields in line Anldra12 9 583 Jun-15-2021, 08:16 AM
Last Post: Anldra12
  Plz trace this line by line Sure 2 269 Jun-12-2021, 08:29 AM
Last Post: ibreeden
  how long can a line of code be? Skaperen 2 331 Jun-09-2021, 06:31 PM
Last Post: Skaperen
  beginner text formatting single line to column jafrost 4 835 Apr-28-2021, 07:03 PM
Last Post: jafrost
  If match not found print last line tester_V 2 759 Apr-26-2021, 05:18 AM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020