Python: String Operation: Difference between revisions

From OnnoCenterWiki
Jump to navigationJump to search
Onnowpurbo (talk | contribs)
Created page with " ==Strip /n== line.strip() will remove all types of whitespaces from both ends of the line. You can use line.rstrip("\n") to remove only the trailing "\n". ==Join Line==..."
 
Onnowpurbo (talk | contribs)
 
(4 intermediate revisions by the same user not shown)
Line 4: Line 4:
==Strip /n==
==Strip /n==


line.strip() will remove all types of whitespaces from both ends of the line.
* line.strip() - remove all types of whitespaces from both ends of the line.
You can use line.rstrip("\n") to remove only the trailing "\n".
* line.rstrip("\n") remove only the trailing "\n".
 
 
==Hitung banyak word==
 
len(string.split())




Line 12: Line 17:
Pakai str.join:
Pakai str.join:


with open('file.txt') as f:
with open('file.txt') as f:
    print " ".join(line.strip() for line in f)
    print " ".join(line.strip() for line in f)
 
 
==Remove specific line==
 
f = open("yourfile.txt","r")
lines = f.readlines()
f.close()
f = open("yourfile.txt","w")
for line in lines:
  if line!="nickname_to_delete"+"\n":
    f.write(line)
f.close()
 
 
 
==Percobaan==
 
namafile = "yourfile.txt"
f = open(namafile,"r")
lines = f.readlines()
f.close()
f = open(namafile,"w")
for line in lines:
  if len(line.split())>1:
    f.write(line)
f.close()
 
f = open(namafile,"r")
    print " ".join(line.strip() for line in f)
f.close()
 
 
==Frequency word==
 
list1 = []    #this is your original list of words
list2 = []    #this is a new list
for word in list1:
    if word in list2:
        list2.index(word)[1] += 1
    else:
        list2.append([word,0])
 
Or, more efficiently:
 
for word in list1:
    try:
        list2.index(word)[1] += 1
    except:
        list2.append([word,0])
 
 
==Frequency word==
 
from collections import Counter
import re
 
words = re.findall(r'\w+', open('hasilbrowsepilkadadki.txt').read().lower())
Counter(words).most_common(10)
[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
('you', 554),  ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]

Latest revision as of 23:38, 1 February 2017


Strip /n

  • line.strip() - remove all types of whitespaces from both ends of the line.
  • line.rstrip("\n") remove only the trailing "\n".


Hitung banyak word

len(string.split())


Join Line

Pakai str.join:

with open('file.txt') as f:
    print " ".join(line.strip() for line in f)


Remove specific line

f = open("yourfile.txt","r")
lines = f.readlines()
f.close()

f = open("yourfile.txt","w")
for line in lines:
  if line!="nickname_to_delete"+"\n":
    f.write(line)
f.close()


Percobaan

namafile = "yourfile.txt"
f = open(namafile,"r")
lines = f.readlines()
f.close()

f = open(namafile,"w")
for line in lines:
  if len(line.split())>1:
    f.write(line)
f.close()
f = open(namafile,"r")
    print " ".join(line.strip() for line in f)
f.close()


Frequency word

list1 = []    #this is your original list of words
list2 = []    #this is a new list

for word in list1:
    if word in list2:
        list2.index(word)[1] += 1
    else:
        list2.append([word,0])

Or, more efficiently:

for word in list1:
    try:
        list2.index(word)[1] += 1
    except:
        list2.append([word,0])


Frequency word

from collections import Counter
import re
words = re.findall(r'\w+', open('hasilbrowsepilkadadki.txt').read().lower())
Counter(words).most_common(10)

[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),

('you', 554),  ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]