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
  How to add multi-line comment section? Winfried 1 138 Mar-24-2024, 04:34 PM
Last Post: deanhystad
  break print_format lengthy line akbarza 4 273 Mar-13-2024, 08:35 AM
Last Post: akbarza
  Reading and storing a line of output from pexpect child eagerissac 1 4,144 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  coma separator is printed on a new line for some reason tester_V 4 417 Feb-02-2024, 06:06 PM
Last Post: tester_V
  problem with spliting line in print akbarza 3 335 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  Unable to understand the meaning of the line of code. jahuja73 0 270 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  Receive Input on Same Line? johnywhy 8 607 Jan-16-2024, 03:45 AM
Last Post: johnywhy
  Reading in of line not working? garynewport 2 784 Sep-19-2023, 02:22 PM
Last Post: snippsat
  Function to count words in a list up to and including Sam Oldman45 15 6,407 Sep-08-2023, 01:10 PM
Last Post: Pedroski55
  'answers 2' is not defined on line 27 0814uu 4 668 Sep-02-2023, 11:02 PM
Last Post: 0814uu

Forum Jump:

User Panel Messages

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