Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Number of characters
#1
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
Quote
#2
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!')
Quote
#3
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
Quote
#4
Try to figure this one out. it's pretty simple
Quote
#5
(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?
Quote
#6
show the code you tried.
Quote
#7
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
Quote
#8
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
Quote
#9
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
Quote
#10
Yea
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Split Column Text by Number of Characters cgoldstein 3 505 Mar-11-2019, 01:45 PM
Last Post: perfringo
  Counting number of characters in a string Drone4four 1 838 Aug-16-2018, 02:33 PM
Last Post: ichabod801
  How can i restrict the number of characters in an input? kevencript 1 659 May-23-2018, 05:14 AM
Last Post: micseydel
  Regex: How to say 'any number of characters of any type until x'? JoeB 2 833 Jan-24-2018, 03:30 PM
Last Post: Mekire
  The number of occurrences of statistical characters RobertW 3 1,863 Jan-18-2018, 10:10 PM
Last Post: nilamo

Forum Jump:


Users browsing this thread: 1 Guest(s)