Jump to content

Words into a list

Wictorian

SO I have many words in a flle line by line and I  wanna put them into a list. I know there is a shortcut for it. How can I do it?

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Wictorian said:

SO I have many words in a flle line by line and I  wanna put them into a list. I know there is a shortcut for it. How can I do it?

How about mentioning the programming-language you're using, for example? This kind of question is kind of useless without you mentioning any of the underlying details.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

make a string array, you can scan character by character and store them into a string, until you find a " " then you move to next string in the array, probably?

-sigh- feeling like I'm being too negative lately

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, WereCatf said:

How about mentioning the programming-language you're using, for example? This kind of question is kind of useless without you mentioning any of the underlying details.

python. sorry forgot.

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, Wictorian said:

SO I have many words in a flle line by line and I  wanna put them into a list. I know there is a shortcut for it. How can I do it?

Here:

wordsFile = open('myfile.txt', 'r') 
words = wordsFile.readlines() 

# Strips whitespace from around the word and prints it
for word in words: 
    print("Word: {}".format(word.strip()))

 

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, Moonzy said:

make a string array, you can scan character by character and store them into a string, until you find a " " then you move to next string in the array, probably?

I dont think what you say is possible, this is what my words look like:

words.png.c8dde9e14b89b769c1bc9d6ec509eecd.png

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, WereCatf said:

Here:


wordsFile = open('myfile.txt', 'r') 
words = wordsFile.readlines() 

# Strips whitespace from around the word and prints it
for word in words: 
    print("Word: {}".format(word.strip()))

 

thx, this way I don't even need to make a list. REALLY appreciate it

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, WereCatf said:

Here:


wordsFile = open('myfile.txt', 'r') 
words = wordsFile.readlines() 

# Strips whitespace from around the word and prints it
for word in words: 
    print("Word: {}".format(word.strip()))

 

after the list ends it returnss blank, how can I control this? 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Wictorian said:

after the list ends it returnss blank, how can I control this? 

Check the length of the strip()ped word and only print it if it's longer than zero.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

Alternatively, you can read the whole file in a string, and then use whatever function splits a string into an array of strings to separate the words. 

 

In php the function is called explode

 

<?php

// read the whole file into the variable $content

$content = file_get_contents('dictionary.txt');

// 0x0D and 0x0A are the two bytes (invisible characters) that form the 
// ENTER sequence. 
// CR (0x0D) is Carriage Return - return cursor to first character in line
// LF (0x0A) is Line Feed - advance cursor on next line
// Some editors only insert the LF character, so often it makes sense to split 
// only using that byte and then strip any CR byte from the end of words 

$words = explode( chr(0x0D).chr(0x0A) , $content);

// print all words 

foreach ($words as $word) {
	if (strlen($word)>0) echo $word . "\n";
}

?>

 

Link to comment
Share on other sites

Link to post
Share on other sites

if you use list comprehension you don't get the  blank line

 

words = [line.strip() for line in open('payments.csv', 'r')]
for word in words:
  print(f'{word}')

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×