Python Forum
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
Reply
#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!')
Reply
#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
Reply
#4
Try to figure this one out. it's pretty simple
Reply
#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?
Reply
#6
show the code you tried.
Reply
#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
Reply
#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
Reply
#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
Reply
#10
Yea
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Find if chain of characters or number Frankduc 4 1,751 Feb-11-2022, 01:55 PM
Last Post: Frankduc
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,416 May-15-2020, 01:37 PM
Last Post: snippsat
  Split Column Text by Number of Characters cgoldstein 3 2,948 Mar-11-2019, 01:45 PM
Last Post: perfringo
  Counting number of characters in a string Drone4four 1 3,410 Aug-16-2018, 02:33 PM
Last Post: ichabod801
  How can i restrict the number of characters in an input? kevencript 1 3,983 May-23-2018, 05:14 AM
Last Post: micseydel
  Regex: How to say 'any number of characters of any type until x'? JoeB 2 2,346 Jan-24-2018, 03:30 PM
Last Post: Mekire
  The number of occurrences of statistical characters RobertW 3 5,199 Jan-18-2018, 10:10 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020