พึ่งหัดเขียนได้ 3 วัน ครับ พอดีผมอยากแยกข้อมูลใน line ที่ตรงกับข้อมูลใน list ไปไว้อีกไฟล์
แล้วก็ ข้อมูลใน line ที่ไม่ตรงกับข้อมูลใน list ไปไว้อีกไฟล์นึงครับ
ตอนนี้แยก ข้อมูลที่ตรงกับข้อมูลใน list ไปอีกไฟล์ได้แล้ว แต่ส่วนที่เหลือไปไม่เป็นแล้วครับ :)

ตัวอย่างโค๊ดที่เขียนครับ

file_name = "output_file.txt"

tower_emp = ["02721","70015","70041","70029","70059","70065","70111","70081",
"70166","70193","70122","70153","70203","70213","70263","70221"]

tower = "tower.txt"
trading = "trading.txt"

input_file = open(file_name, 'r')
lines = input_file.readlines()
tower_file = open(tower, 'w')
trading_file = open(trading, 'w')

for line in lines:
for i in range(len(tower_emp)):
if tower_emp[i] in line:
tower_data = line
tower_file.write(tower_data)
tower_file.close()

Hiring! บริษัทที่น่าสนใจ

Carmen Software company cover
Carmen Software
Hotel Financial Solutions
Next Innovation (Thailand) Co., Ltd. company cover
Next Innovation (Thailand) Co., Ltd.
We are web design with consulting & engineering services driven the future stronger and flexibility.
KKP Dime company cover
KKP Dime
KKP Dime บริษัทในเครือเกียรตินาคินภัทร
Kiatnakin Phatra Financial Group company cover
Kiatnakin Phatra Financial Group
Financial Service
Fastwork Technologies company cover
Fastwork Technologies
Fastwork.co เว็บไซต์ที่รวบรวม ฟรีแลนซ์ มืออาชีพจากหลากหลายสายงานไว้ในที่เดียวกัน
Thoughtworks Thailand company cover
Thoughtworks Thailand
Thoughtworks เป็นบริษัทที่ปรึกษาด้านเทคโนโยลีระดับโลกที่คว้า Great Place to Work 3 ปีซ้อน
Iron Software company cover
Iron Software
Iron Software is an American company providing a suite of .NET libraries by engineer for engineers.
CLEVERSE company cover
CLEVERSE
Cleverse is a Venture Builder. Our team builds several tech companies.
Nipa Cloud company cover
Nipa Cloud
#1 OpenStack cloud provider in Thailand with our own data center and software platform.
Bangmod Enterprise company cover
Bangmod Enterprise
The leader in Cloud Server and Hosting in Thailand.
CIMB THAI Bank company cover
CIMB THAI Bank
MOVING FORWARD WITH YOU - CIMB is the leading ASEAN Bank
Bangkok Bank company cover
Bangkok Bank
Bangkok Bank is one of Southeast Asia's largest regional banks, a market leader in business banking
MuvMi (Urban Mobility Tech Co.,Ltd.) company cover
MuvMi (Urban Mobility Tech Co.,Ltd.)
Shape the future of urban mobility towards affordable, clean, and safe solutions
T.N. Digital Solution Co., Ltd. company cover
T.N. Digital Solution Co., Ltd.
TNDS has been involving in every first move of banking’s major digital transformation.
KBTG - KASIKORN Business-Technology Group company cover
KBTG - KASIKORN Business-Technology Group
KBTG - "The Technology Company for Digital Business Innovation"
Siam Commercial Bank Public Company Limited company cover
Siam Commercial Bank Public Company Limited
"Let's start a brighter career future together"
Icon Framework co.,Ltd. company cover
Icon Framework co.,Ltd.
Global Standard Platform for Real Estate แพลตฟอร์มสำหรับธุรกิจอสังหาริมทรัพย์ครบวงจร มาตรฐานระดับโลก
REFINITIV company cover
REFINITIV
The Financial and Risk business of Thomson Reuters is now Refinitiv
H LAB company cover
H LAB
Re-engineering healthcare systems through intelligent platforms and system design.
The Gang Technology Co., Ltd. company cover
The Gang Technology Co., Ltd.
We're a Digital Agency that helps our customers transform their business into digital with ease.
LTMH company cover
LTMH
LTMH มุ่งเน้นการพัฒนาผลิตภัณฑ์ที่สามารถช่วยพันธมิตรของเราให้บรรลุเป้าหมาย
Seven Peaks company cover
Seven Peaks
We Drive Digital Transformation
Wisesight (Thailand) Co., Ltd. company cover
Wisesight (Thailand) Co., Ltd.
The Best Choice For Handling Social Media · High Expertise in Social Data · Most Advanced and Secure
MOLOG Tech company cover
MOLOG Tech
We are Modern Logistic Platform, Specialize in WMS, OMS and TMS.
Data Wow Co.,Ltd company cover
Data Wow Co.,Ltd
We enable our clients to realize increased productivity by solving their most complex issues by Data
LINE Company Thailand company cover
LINE Company Thailand
LINE, the world's hottest mobile messaging platform, offers free text and voice messaging + Call
LINE MAN Wongnai company cover
LINE MAN Wongnai
Join our journey to becoming No.1 food platform in Thailand

ไม่แน่ใจว่าผมเข้าใจปัญหาถูกหรือปล่าว(ส่วนนึงเพราะไม่ได้ indent code มาด้วย) แต่เท่าที่ดูจาก code เข้าใจว่าขาดแค่


else:
    trading_data = line
    trading_file.write(trading_data)
    trading_file.close()

ยังไงก็ตามถ้าผมเข้าใจโจทย์และ indent ไม่ผิด flow แบบที่คุณเขียนอาจทำให้เกิดการเขียนข้อมูล line ไปที่ไฟล์ tower_file/trading_file ซ้ำๆ ได้ในกรณีที่มีข้อมูลใน tower_emp ใน line มากกว่าหนึ่งตัว

อีกข้อสังเกตนึงคือโค้ดอาจจะยาวเกินจำเป็นไปนิดนึงหรือปล่าวครับ ผมอ่านโจทย์ดูแล้วลองปรับเป็นตามด้านล่าง(อาจจะสั้นเกินไปเหมือนกัน มาตามอ่านทีหลัง มีโอกาสงง)


tower_emp = ["02721","70015","70041","70029","70059","70065","70111","70081","70166","70193","70122","70153","70203","70213","70263","70221"]
with file('tower.txt','w') as tower:
  with file('trading.txt','w') as trading:
    map(lambda line: [ret for ret in tower_emp if ret in line] and [tower.write(line)] or trading.write(line),file('output_file.txt','r').readlines())

ฝากรบกวนลองเอาไปรันและแจ้งผลนิดนึงนะครับ คือลองปรับแล้วแต่ไม่มีข้อมูลทดสอบ(output_file.txt) อยากรู้ว่ามันเวิร์กหรือปล่าว

ผมก็คิดว่าโค๊ดยาวเกินจำเป็น อ๊าย อาย แหะๆ แต่อย่างที่บอกครับ พึ่งหัดเขียนได้ วันที่ 3 นี้จริงๆ

ผลที่ได้คือ ข้อมูลที่ trading จะเบิ้ลเท่ากับ จำนวนสมาชิกใน list เลยครับ แล้วก็บรรทัดที่มีข้อมูลตรงกับสมาชิกใน List ก็ยังโผล่ที่ trading.txt ครับ
ตัวอย่างข้อมลใน output_file.txt ครับ (เป็นข้อมูลที่ดึงมาจากกล่อง TAFF เครื่องรูดบัตรครับ)

70111 21/11/2552 1800 01
70153 21/11/2552 1804 01
70081 21/11/2552 1834 01
70213 21/11/2552 1901 01
15916 21/11/2552 0713 10
18841 21/11/2552 0723 10
06385 21/11/2552 0734 10
70081 21/11/2552 0754 10
20826 21/11/2552 0906 10
18841 21/11/2552 1700 10
06385 21/11/2552 1700 10
15916 21/11/2552 1700 10
20826 21/11/2552 1707 10
code

file_name = "output_file.txt"
tower_file = "tower.txt"
trading_file = "trading.txt"
tower_emp = ["02721","70015","70041","70029","70059","70065","70111","70081",
"70166","70193","70122","70153","70203","70213","70263","70221"]

input_file = open(file_name, 'r')
lines = input_file.readlines()
output_tower_file = open(tower_file, 'w')
output_trading_file = open(trading_file, 'w')
for line in lines:
    for i in range(len(tower_emp)):
        if tower_emp[i] in line:
            tower_data = line
            output_tower_file.write(tower_data)
        else:
            trading_data = line
            output_trading_file.write(trading_data)
output_tower_file.close

ผมว่าที่ผิดเพราะคุณวนลูป tower_emp แล้วมาเทียบกับ line ที่วนลูปอยู่อีกทีอะครับ
ถ้าเอา line มาเทียบกับ tower_emp ไปเลย น่าจะดีกว่านะครับ


for line in lines:
    if line.split()[0] in tower_emp:
        output_tower_file.write(line)
    else:
        output_trading_file.write(line)