Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99595 > unrolled thread
| Started by | vincentypedro@gmail.com |
|---|---|
| First post | 2015-11-26 12:34 -0800 |
| Last post | 2015-11-26 21:44 -0500 |
| Articles | 6 — 6 participants |
Back to article view | Back to comp.lang.python
reading from a txt file vincentypedro@gmail.com - 2015-11-26 12:34 -0800
Re: reading from a txt file Denis McMahon <denismfmcmahon@gmail.com> - 2015-11-26 20:50 +0000
Re: reading from a txt file Jason Friedman <jsf80238@gmail.com> - 2015-11-26 14:04 -0700
Re: reading from a txt file Gary Herron <gherron@digipen.edu> - 2015-11-26 13:01 -0800
Re: reading from a txt file Pedro Vincenty <vincentypedro@gmail.com> - 2015-11-26 17:24 -0800
Re: reading from a txt file Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-11-26 21:44 -0500
| From | vincentypedro@gmail.com |
|---|---|
| Date | 2015-11-26 12:34 -0800 |
| Subject | reading from a txt file |
| Message-ID | <175ab5d2-d8e3-44e7-a71f-88b3153daf89@googlegroups.com> |
Hey, I'm wondering how to read individual strings in a text file. I can read a text file by lines with .readlines() , but I need to read specifically by strings, not including spaces. Thanks in advance
[toc] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-11-26 20:50 +0000 |
| Message-ID | <n37rah$h02$8@dont-email.me> |
| In reply to | #99595 |
On Thu, 26 Nov 2015 12:34:36 -0800, vincentypedro wrote:
> Hey, I'm wondering how to read individual strings in a text file. I can
> read a text file by lines with .readlines() ,
> but I need to read specifically by strings, not including spaces.
> Thanks in advance
How do you define a string? Is it just a line with the spaces removed?
>>> "".join(" this is a test string my friends ".split(" "))
'thisisateststringmyfriends'
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Jason Friedman <jsf80238@gmail.com> |
|---|---|
| Date | 2015-11-26 14:04 -0700 |
| Message-ID | <mailman.153.1448571876.20593.python-list@python.org> |
| In reply to | #99595 |
>
> Hey, I'm wondering how to read individual strings in a text file. I can
> read a text file by lines with .readlines() ,
> but I need to read specifically by strings, not including spaces. Thanks
> in advance
>
How about:
for a_string in open("/path/to/file").read().split():
print(a_string)
[toc] | [prev] | [next] | [standalone]
| From | Gary Herron <gherron@digipen.edu> |
|---|---|
| Date | 2015-11-26 13:01 -0800 |
| Message-ID | <mailman.154.1448572207.20593.python-list@python.org> |
| In reply to | #99595 |
On 11/26/2015 12:34 PM, vincentypedro@gmail.com wrote: > Hey, I'm wondering how to read individual strings in a text file. I can read a text file by lines with .readlines() , > but I need to read specifically by strings, not including spaces. Thanks in advance Read the lines with readlines(), as you say, then split each line into whatever pieces you want with split (or with any of the many other string methods). A minimal example (without readlines): >>> lines = ['a b c', 'aa bb cc'] >>> for line in lines: ... words = line.split() ... print(words) ... ['a', 'b', 'c'] ['aa', 'bb', 'cc'] -- Dr. Gary Herron Department of Computer Science DigiPen Institute of Technology (425) 895-4418
[toc] | [prev] | [next] | [standalone]
| From | Pedro Vincenty <vincentypedro@gmail.com> |
|---|---|
| Date | 2015-11-26 17:24 -0800 |
| Message-ID | <1e567393-3750-43f2-8e7c-6e92f687ce9f@googlegroups.com> |
| In reply to | #99595 |
All were really helpful thanks a lot. Now I'm interested in identifying a particular index after being able to print out each word. Printing each word to the console I have : ['METEOSAT-7'] ['1', '24932U', '97049B', '15319.57839525', '.00000058', '00000-0', '00000+0', '0', '9994'] ['2', '24932', '9.9015', '42.7484', '0001500', '224.8381', '52.7416', '1.00266716', '66645'] It seems awkward because I was expecting to have brackets only outside of all the words. I would like to identify one of these words. Thanks again!
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2015-11-26 21:44 -0500 |
| Message-ID | <mailman.161.1448592253.20593.python-list@python.org> |
| In reply to | #99612 |
On Thu, 26 Nov 2015 17:24:38 -0800 (PST), Pedro Vincenty
<vincentypedro@gmail.com> declaimed the following:
>All were really helpful thanks a lot. Now I'm interested in identifying a particular index after being able to print out each word. Printing each word to the console I have :
>
>['METEOSAT-7']
>['1', '24932U', '97049B', '15319.57839525', '.00000058', '00000-0', '00000+0', '0', '9994']
>['2', '24932', '9.9015', '42.7484', '0001500', '224.8381', '52.7416', '1.00266716', '66645']
>
>It seems awkward because I was expecting to have brackets only outside of all the words. I would like to identify one of these words. Thanks again!
It would help to have the sample of the input file and the code used on
it that generates this result.
I see:
* a one element list
* a nine element list -- with some elements having invalid formats for
Python numbers
* a nine element list, though all fields can be converted to numeric
types
This could be the result of printing each "line" of words, but not from
printing each "word" itself.
>>> INPUT = "METEOSAT-7\n1 24932U 97049B 15319.57839525 .00000058 00000-0 00000+0 0 9994\n2 24932 9.9015 42.7484 0001500 224.8381 52.7416 1.00266716 66645"
>>>
>>> for ln in INPUT.split("\n"):
... words = ln.split()
... print ln
... print words
... for wd in words:
... print wd
...
METEOSAT-7
['METEOSAT-7']
METEOSAT-7
1 24932U 97049B 15319.57839525 .00000058 00000-0 00000+0 0 9994
['1', '24932U', '97049B', '15319.57839525', '.00000058', '00000-0',
'00000+0', '0', '9994']
1
24932U
97049B
15319.57839525
.00000058
00000-0
00000+0
0
9994
2 24932 9.9015 42.7484 0001500 224.8381 52.7416 1.00266716 66645
['2', '24932', '9.9015', '42.7484', '0001500', '224.8381', '52.7416',
'1.00266716', '66645']
2
24932
9.9015
42.7484
0001500
224.8381
52.7416
1.00266716
66645
>>>
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web