مالفرق ؟؟؟

abdulrahman-abdullah • منذ 6 سنوات

السلام عليكم ور حمة الله وبركاته ،، أسعد الله أوقاتكم بكل خير ... 

في البداية سؤالي فضولي فقط ،، وليس مقارنة بين لغتين !!!

كتبت كود يقرأ البيانات من صفحة url  بإستخدام الجافا ،، وبعدين قلت بجرب بإستخدام البايثون .. 

بعد التنفيذ للكودين ، حصلت فرق في النتيجة ،

البايثون يجلب كل كملة على حد ، أما الجافا تجلب سطر كامل ؟؟ ما سبب ؟ 

java code 



public class Main {

    public static void main(String[] args) {
	// write your code here
        try {
            URL url = new URL("http://sixty-north.com/c/t.txt");
            URLConnection connection = url.openConnection();
            InputStream inStream = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
            String line = null ;
            int i = 0 ;
            while ((line = reader.readLine()) != null){
                i++;
                System.out.println(i + "-"+line);
            }
        } catch(MalformedURLException  ex){

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

java result 


1-It was the best of times
2-it was the worst of times
3-it was the age of wisdom
4-it was the age of foolishness
5-it was the epoch of belief
6-it was the epoch of incredulity
7-it was the season of Light
8-it was the season of Darkness
9-it was the spring of hope
10-it was the winter of despair
11-we had everything before us
12-we had nothing before us
13-we were all going direct to Heaven
14-we were all going direct the other way
15-in short the period was so far like the present period that some of
16-its noisiest authorities insisted on its being received for good or for
17-evil in the superlative degree of comparison only

Process finished with exit code 0

python code 


import this
from urllib.request import urlopen 

def fetch_words():
    i = 0 
    with urlopen("http://sixty-north.com/c/t.txt") as story : 
        story_words =[]
        
        for line in story :
            line_words = line.decode("utf-8").split()
            for word in line_words:
                
                story_words.append(word)
                
    for word in story_words:
        i += 1
        print(i , word)



fetch_words() 

 

python result 


1 It
2 was
3 the
4 best
5 of
6 times
7 it
8 was
9 the
10 worst
11 of
12 times
13 it
14 was
15 the
16 age
17 of
18 wisdom
19 it
20 was
21 the
22 age
23 of
24 foolishness
25 it
26 was
27 the
28 epoch
29 of
30 belief
31 it
32 was
33 the
34 epoch
35 of
36 incredulity
37 it
38 was
39 the
40 season
41 of
42 Light
43 it
44 was
45 the
46 season
47 of
48 Darkness
49 it
50 was
51 the
52 spring
53 of
54 hope
55 it
56 was
57 the
58 winter
59 of
60 despair
61 we
62 had
63 everything
64 before
65 us
66 we
67 had
68 nothing
69 before
70 us
71 we
72 were
73 all
74 going
75 direct
76 to
77 Heaven
78 we
79 were
80 all
81 going
82 direct
83 the
84 other
85 way
86 in
87 short
88 the
89 period
90 was
91 so
92 far
93 like
94 the
95 present
96 period
97 that
98 some
99 of
100 its
101 noisiest
102 authorities
103 insisted
104 on
105 its
106 being
107 received
108 for
109 good
110 or
111 for
112 evil
113 in
114 the
115 superlative
116 degree
117 of
118 comparison
119 only

سؤالي مثل ما ذكرت ، لماذا البايثون يجلب لي كلمة على حد ، لماذا لا تجلب سطر كامل ؟ 

كلمات دليلية:

ساعد بالإجابة

"إن في قضاء حوائج الناس لذة لا يَعرفها إلا من جربها، فافعل الخير مهما استصغرته فإنك لا تدري أي حسنة تدخلك الجنة."

الإجابات (2)

سطام • منذ 6 سنوات

split افتراضياً بدون بارامترات تفصل النص بقائمة list بناء على المسافات

المفروض تستخدم


split('\n') 

 

وإذا أردت كود مختصر كما يلي:


from urllib.request import urlopen 

def fetch_words():
    story = urlopen("http://sixty-north.com/c/t.txt").read().decode('utf-8').split('\n')
    for i, line in enumerate(story, 1):
        print(i, line)

fetch_words() 

 

abdulrahman-abdullah • منذ 6 سنوات

ممتاز ، شكرا حبيبي سطام ، كأن فرق في الـ syntax  

لايوجد لديك حساب في عالم البرمجة؟

تحب تنضم لعالم البرمجة؟ وتنشئ عالمك الخاص، تنشر المقالات، الدورات، تشارك المبرمجين وتساعد الآخرين، اشترك الآن بخطوات يسيرة !