Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Wictorian

Words into a list

Recommended Posts

Posted · Original PosterOP

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 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 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?


Things I need help with: *new* What can Facebook do to me? Privacy Inquiries

Spoiler

 

 

I hate Intel's pricing, Ryzen's weird quirks, Nvidia's pricing, and Radeon GPUs in general

 

Spoiler

 

Products I like:

Spoiler

Sony Xperia Z1 / Z2 / 10 ii, Asus Strix 970 / 1070, Samsung SSD, WD HDD, Corsair PSUs (AX, RM, CX(grey)), GeForce GPU, NZXT N450/S340, be quiet! Coolers, G.Skill Trident RAM, Logitech M525, Logitech G440, Razer Deathadder Elite

 

Products I hate:

Spoiler

Xperia Z3, XiaoMi 5c, Radeon GPUs, Razer Audio Products, any bloatwares

 

Companies I absolutely adore: (and hope it stays that way)

Spoiler

be quiet! - sent me AM4 mounting for my DRP3 even though it's way past the timeframe stated, no questions asked

Corsair - very good RMA experience, absolutely recommend

 

Companies I hate:

Spoiler

Nvidia, Intel, Apple, TMT (Thundermatch, a retailer)

 

Personal Blacklisted Companies:

Spoiler

Acer monitors, Gigabyte, Seagate HDD, Kingston SSD, Razer (except their mouse), XiaoMi Phones

 

Remember, just because I had good/bad experiences with these companies/product, doesn't mean you will have similar experiences too. I would still recommend these products if they made sense for your needs, but I'll add a disclaimer of my experience if it's relevant. Feel free to DM me asking why they are where they are.

 

 

Link to post
Share on other sites
Posted · Original PosterOP
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 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 post
Share on other sites
Posted · Original PosterOP
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 post
Share on other sites
Posted · Original PosterOP
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 post
Share on other sites
Posted · Original PosterOP
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 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 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 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 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


×