ฉันจะรับสตริงสุ่มจากไฟล์ .txt ได้อย่างไร

ฉันเพิ่งเริ่มเขียนโปรแกรมด้วย python 2.7 และฉันมีปัญหากับตัวสร้างแบบสุ่มในการรับไอเท็มจากไฟล์ .txt ภายนอก สงสัยว่าจะมีใครช่วยฉันหน่อยได้ไหม...

โดยพื้นฐานแล้ว ฉันต้องการสุ่มการเข้าชม 100 ครั้ง และนับทั้งหมดในตอนท้าย และพิมพ์ข้อมูลสรุปสำหรับสตริง ฉันหวังว่ามันจะสมเหตุสมผล!

ฉันรู้ว่าคำถามนี้เคยได้รับคำตอบมาก่อน แต่คำตอบไม่ได้ช่วยฉันเลย

ดังนั้นไฟล์ txt ของฉันจึงมีสตริงจำนวนหนึ่ง โดยคั่นด้วยเครื่องหมายจุลภาค นี่คือลักษณะของรหัสของฉัน:

import random

fname = raw_input("Enter filename: ")
infile = open(fname,'r')
data = infile.readlines()

for i in range(100):
   print(random.uniform("Car_types.txt"))

ขอบคุณสำหรับความช่วยเหลือของคุณล่วงหน้า!


person Odani87    schedule 08.04.2016    source แหล่งที่มา


คำตอบ (3)


ฉันเพิ่งใช้ไฟล์ข้อความเพื่อทดสอบโค้ดของฉัน ไฟล์ทดสอบมีลักษณะดังนี้ (ไม่แน่ใจว่าไฟล์ของคุณอยู่ในรูปแบบเดียวกับของฉันหรือไม่)

import random
def addElements(l):
    for i in l:
        global result
        result.append(i)
fname=raw_input("Enter filename: ")
result=[]
summary=[]
for line in open(fname,'r'):
    elements=line.strip().split(',')
    addElements(elements)
for i in range(100):
    summary.append(random.choice(result))
print summary
  1. ทดสอบ.txt:

เอบีซี def กี

jkl,mno,pqr

  1. ผลลัพธ์:

['mno', 'ghi', 'pqr', 'jkl', 'abc', 'abc', 'jkl', 'jkl', 'jkl', 'def', 'pqr', 'def', ' jkl', 'pqr', 'mno', 'jkl', 'mno', 'def', 'jkl', 'jkl', 'def', 'mno', 'pqr', 'pqr', 'abc' , 'pqr', 'def', 'abc', 'pqr', 'def', 'jkl', 'pqr', 'mno', 'mno', 'mno', 'def', 'jkl', ' def', 'ghi', 'jkl', 'pqr', 'mno', 'ghi', 'mno', 'mno', 'jkl', 'jkl', 'ghi', 'abc', 'ghi' , 'ghi', 'jkl', 'mno', 'def', 'ghi', 'ghi', 'abc', 'abc', 'pqr', 'ghi', 'abc', 'abc', ' mno', 'def', 'jkl', 'mno', 'mno', 'jkl', 'jkl', 'pqr', 'pqr', 'pqr', 'jkl', 'def', 'jkl' , 'mno', 'mno', 'ghi', 'jkl', 'mno', 'ghi', 'pqr', 'abc', 'ghi', 'def', 'ghi', 'ghi', ' jkl', 'def', 'abc', 'abc', 'ghi', 'jkl', 'pqr', 'pqr', 'pqr', 'def', 'pqr', 'pqr', 'def' ]

หวังว่านี่จะช่วยคุณได้

หากยังคงพิมพ์เรื่องไร้สาระออกมาให้คุณ โปรดแจ้งให้เราทราบว่าไฟล์ txt ของคุณมีลักษณะอย่างไร ขอบคุณ!

person Michael Gao    schedule 08.04.2016

นี่อาจเป็นสิ่งที่คุณกำลังมองหา:

import random

fname = raw_input("Enter filename: ")
# With statements are better as they explicitly close the file
with open(fname,'r') as infile:
    data = infile.read().split(",")  # Split the strings with commas as you said

for i in range(100):
   print(random.choice(data))  # This will randomly choose one of the strings
person Bharel    schedule 08.04.2016
comment
สวัสดีบาเรล! ขอบคุณสำหรับคำแนะนำของคุณ แต่ฉันกลับกลายเป็นเรื่องไร้สาระ นี่คือสิ่งที่ฉันได้รับ:Enter filename: Car_types.txt . เอ็กซ์ ที C p t C y t t r er t . ใช่แล้ว - person Odani87; 08.04.2016
comment
@Odani ฉันไม่แน่ใจว่าเป็นไปได้อย่างไร - person Bharel; 08.04.2016

หากคุณไม่ต้องการได้รับค่าเดียวกัน ให้ใช้ ตัวอย่าง หลังจากแยกข้อมูลด้วยเครื่องหมายจุลภาค:

fname = raw_input("Enter filename: ")
with  open(fname) as f:
    for r in random.sample(f.read().split(), 100):
        print(r)

คุณกำลังเปิดไฟล์ เรียก readlines แล้วไม่เคยใช้เนื้อหาเลย random.uniform("Car_types.txt")) ดำเนินการกับสตริงตัวอักษร "Car_types.txt" คุณต้องใช้เนื้อหาไฟล์จริง

person Padraic Cunningham    schedule 08.04.2016
comment
มันใช้งานได้ แต่ฉันยังนึกไม่ออกว่าฉันจะมีเอาต์พุตสรุปได้อย่างไร - ทุกสตริงนับหมด - person Odani87; 12.04.2016