Python Forum

Full Version: Number of characters
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, im begginer in python. I have a problem with cycle.

i have text.txt
Quote:Text
The text denotes any combination of words that together make a statement.
Mostly, however, the text is denoted by some separate or paragraph.
The word text is used for written statements.
Books are labeled as artistic texts, newspaper articles as publicity texts,

work.py

import os 
import sys

cesta_vstup_rel = 'text.txt' 

cesta_vystup_rel = 'text_vystup.txt'

if not os.path.exists(cesta_vstup_rel): 
    print 'vstupni soubor nenalezen!'
    sys.exit() 

vstup = open(cesta_vstup_rel, 'r') 
radky = vstup.readlines() 
vstup.close() 

zapis = open(cesta_vystup_rel, 'w') #oteviram soubor pro zapis (w-write)

for radek in radky:
    zapis.write(radek.strip('\n') + '[b]number of characters[/b]')

zapis.close()
i need make numbers of all characters on end of line, like this:
Quote:Text4
The text denotes any combination of words that together make a statement.73
Mostly, however, the text is denoted by some separate or paragraph.67
The word text is used for written statements.45
Books are labeled as artistic texts, newspaper articles as publicity texts,75

Please help me, i working on it all night... :) Thank you
I fixed your BBCODE tags above, use python icon, not quotes for code.

import os
import sys

try:
    with open('text.txt', 'r') as f, open('text_vystup.txt', 'w') as f1:
        for line in f:
            line = line.strip()
            line = '{}{}\n'.format(line, len(line))
            f1.write(line)
except IOError:
    print('vstupni soubor nenalezen!')
Hello, its pretty simple, clear and smart code. Thanks very much

And please one thing.

if i have in .txt for example:
Quote:black 12
white 10
yellow 8
color 15

how can edit this code for result in .txt what will looks like this with format syntax??? i used "array" for separation "yellow color" Its a good idea?
Quote:sumofblackwhite = 24
color_x_sumofblackwhite = 360
Try to figure this one out. it's pretty simple
(Jan-10-2018, 11:45 AM)Larz60+ Wrote: [ -> ]Try to figure this one out. it's pretty simple

Its simple if white and black color looks

Quote:black = 12
white = 10

I tryed strip(), and its doesnt working

I thinking about array, and summery only int from second cell. Its a good way?
show the code you tried.
It was a bad code, absolutly didnt worked. Please do you know some tutorial on youtube or manual, where i can find how i can choose only int from line? Everywhere are math operations only variables with value (its easy)...

I am desperate from missing equals :-D
If you don't try, you won't learn.
Make an attempt, show what you have, even if it's bad code,
and we will be glad to help
import os
import sys

cesta_vstup = 'text.txt'
cesta_vystup = 'text_out.txt'

if not os.path.exists(text_out.txt):
    print 'vstupni soubor nebyl nalezen!'
    sys.exit()

cti = open(cesta_vstup, 'r')
radky = cti.readlines()
cti.close()

for i in range(0,len(radky)):
    if radky[i][0].isdigit() == False: 
        index_konec_hlavicky = i 
        break

hlavicka = radky[0:index_konec_hlavicky]
telo = radky[index_konec_hlavicky:]

zapis = open(cesta_vystup, 'w')
zapis.writelines(hlavicka)

for radek in radky:
    if 'black' in radek:
        black = radek.split()[1]
print black

for radek in radky:
    if 'white' in radek:
        white = radek.split()[1]
print white

a = black
b = white
c = black + white
print c
= 1122 :-D, i need convert it like int

YES!!!! i have it!!!!!!!!!! int(black)!! :-)))) i am happy :-D
Yea