ÆÄÀ̽㠽ºÅ©¸³Æ® Áú¹®

   Á¶È¸ 3024   Ãßõ 0    

어제 밤 질문 이후에 코드를 계속 수정해보고 있는데요,

아래 왼쪽과 오른쪽 경우 파일을 읽어들이는데 있어서 차이가 납니다.

오른쪽 경우처럼, 반복문을 사용해서 파일을 읽어들이면, 파일의 첫 번째 줄 내용이 안나옵니다.

반복문을 써도 첫 번째 줄 내용도 나오게 할 수 있나요??

이것만 해결되면 완성이 되는데 어렵습니다 ㅠ


ªÀº±Û Àϼö·Ï ½ÅÁßÇÏ°Ô.
chis 2018-12
read_file=rmsf_file.read()
ÈÄ¿¡ ¹Ýº¹¹® µ¹¸®´Â°Ç ¾ÈµÇ³ª¿ä?
     
³×ÀÌÃÄ 2018-12
±×·¸°Ô ÇÏ´Ï Äڵ尡 ÀÛµ¿À» ¾ÈÇϳ׿ä

rmsf_xvg = "rmsf.xvg"

with open(rmsf_xvg, "r") as rmsf_file:
    residue_number = 0
    for data in rmsf_file:
        column = data.split("  0.")[0]
        residue_number += 1

        read_file = rmsf_file.read()
        edit_file = read_file.replace(str(column), str(residue_number).rjust(5))

        with open(rmsf_xvg, "w") as rmsf_file:
            rmsf_file.write(edit_file)

ÀÌ·¸°Ô Çؼ­, ù ¹ø° column ¼ýÀÚµéÀ» 1ºÎÅÍ ´Ù½Ã ½ÃÀÛÇÏ°Ô ¹Ù²Ù·Á°í Çϰŵç¿ä....
¹è»ó0¿ø 2018-12
rmsf_xvg = "rmsf.xvg"
with open(rmsf_xvg, "r") as rmsf_file:
  for read_file in rmsf_file:
    print(read_file)
     
³×ÀÌÃÄ 2018-12
ÀÌ·¸°Ô Çϸé, ù ¹ø° ÁÙÀÎ 5  0.2148  °¡ ÀνÄÀº µÇ´Âµ¥¿ä,

replace¸¦ Çϸé, 5¸¸ 1·Î ¹Ù²î°í ³ª¸ÓÁö´Â ¾È¹Ù²ò´Ï´Ù...
          
¹è»ó0¿ø 2018-12
replace ºÎºÐÀ» ¿Ã·ÁÁÖ½ÃÁÒ.
               
³×ÀÌÃÄ 2018-12
rmsf_xvg = "rmsf.xvg"

with open(rmsf_xvg, "r") as rmsf_file:
    residue_number = 0
    for data in rmsf_file:
        column = data.split("  0.")[0]
        residue_number += 1

        read_file = rmsf_file.read()
        edit_file = read_file.replace(str(column), str(residue_number).rjust(5))

        with open(rmsf_xvg, "w") as rmsf_file:
            rmsf_file.write(edit_file)

ÀÌ·¸°Ô Â¥º¸·Á°í ÇÕ´Ï´Ù~~
±èÁ¦¿¬ 2018-12
for data in rmsf_file:
        column = data.split("  0.")[0]
        residue_number += 1

        read_file = rmsf_file.read()
        edit_file = read_file.replace(str(column), str(residue_number).rjust(5))

        with open(rmsf_xvg, "w") as rmsf_file:
            rmsf_file.write(edit_file)

ÀÌ·¸°Ô ÇÏ¸é ¾ÈµÇ´Â°Å °°Àºµ¥¿ä ..
°è¼Ó.. °¡Á® ¿À½Ã´Â°Å·Î ÀνÄÇϽŰŠ°°Àºµ¥
read_file = rmsf_file.read() ÇϽøé.. Áß°£Áß°£ °Ç³Ê¶Û²®´Ï´Ù.
±×¸®°í ¿À¸¥ÂÊ ¼Ò½º¿¡¼­ .. 2¹ø°ºÎÅÍ °¡Á®¿À´Â°Ç.. data°¡ ù¹ø° ÁÙÀε¥ ... read()¸¦ ÇÏ´Ï µÎ¹ø°ÁÙÀ» Àоî¿Í¼­ ±×·±µí ÇÕ´Ï´Ù.
ÆÄÀ̽ãÀÌ ¾È±ò·Á À־ ...

À­ºÐ ¹è»ó0¿ø ´Ô ¸»¾¸µ¥·Î .. ¿À¸¥ÂÊ ¼Ò½º ±×´ë·Î °¡Á®´Ù ¾²¸é
for data in rmsf_file
    data °¡ ÇÑÁÙ ÇÑÁÙ ÀÔ´Ï´Ù...
read() ÀÌ .. for ¾ÈÂÊ¿¡ ÀÖÀ¸¸é. ¾ûų²®´Ï´Ù.

ùÁÙ -> ¿¡¼­ ... split Çѵڿ¡  µÎ¹ø°ÁÙ Àоî¿Í¼­ ¾û¶×ÇÑ replace ¸¦ Çϴµí?

    for data in rmsf_file:
        column = data.split("  0.")[0]
        edit_file = data.replace(str(column), str(residue_number).rjust(5))

        with open(rmsf_xvg, "w") as rmsf_file:
            rmsf_file.write(edit_file)

ÀÌ·¸°Ô ÇÏ½Ã¸é µÉ²¨ °°³×¿ä ..
     
³×ÀÌÃÄ 2018-12
¸»¾¸ÇϽŴë·Î ¼öÁ¤Çؼ­ Çϴϱî, µ¥ÀÌÅÍ ÆÄÀÏÀÇ Á¦ÀÏ ¸¶Áö¸· ÁÙ ÇÑÁÙ¸¸ ³²¾ÆÀÖ½À´Ï´Ù.¤Ð

rmsf_xvg = "rmsf.xvg"
with open(rmsf_xvg, "r") as rmsf_file:
    residue_number = 0     
    for data in rmsf_file:
        column = data.split("  0.")[0]
        residue_number += 1
        edit_file = data.replace(str(column), str(residue_number).rjust(5))
        with open(rmsf_xvg, "w") as rmsf_file:
            rmsf_file.write(edit_file)
          
±èÁ¦¿¬ 2018-12
with open(rmsf_xvg, "w") as rmsf_file:

¿©±â¼­ ¿©½Ç¶§ w·Î ¿©½ÃÁö ¸¶½Ã°í
with open(rmsf_xvg, "a") as rmsf_file:

·Î Çغ¸¼¼¿ä append ÀÔ´Ï´Ù.. Ãß°¡µÇ´Â°Å¿¹¿ä..
               
³×ÀÌÃÄ 2018-12
Ãß°¡°¡ µÇ¸é ¾ÈµÇ°í ¼ýÀÚµéÀÌ ¹Ù²¸¾ßµÇ¼­ w·Î Çß½À´Ï´Ù~
                    
±èÁ¦¿¬ 2018-12
±×°É »ó°ü¾ø½À´Ï´Ù »õ·Î¿îÆÄÀÏ ¸¸µé°í °è¼Ó Ãß°¡Çϴ°Ŷó

¾Æ¿ì ÆÄÀϸç¹É Ʋ¸®°Ô Á¤ÇØÁֽðí Çغ¸¼¼¿ä

´ÙÇÑ´ÙÀ½ µ¤¾î¾²°Å³ª
                         
³×ÀÌÃÄ 2018-12
¾Æ~ ³Ü ¾Ë°Ú½À´Ï´Ù~
¹Ù·Î ½ÃµµÇغ¸°Ú½À´Ï´Ù~ °¨»çÇÕ´Ï´Ù~
     
³×ÀÌÃÄ 2018-12
ÀÌ°Å´Â ¾îÁ¦ ¹ã¿¡ ¸¸µé¾ú´ø°Çµ¥, ÀÏ´Ü ¿øÇϴ´ë·Î ÆÄÀÏÀº ¼öÁ¤ÀÌ µË´Ï´Ù.
±×·±µ¥, ¹Ýº¹¹® Àü ÈÄ·Î openÀ» µÎ¹øÇÏ´Â ÀÌ»óÇÑ ¸ð¾çÀΰŠ°°¾Æ¼­ ¼öÁ¤ÇÏ·Á°í ÇÏ°íÀÖ½À´Ï´Ù.
(ÀÌ·¸°Ô ÇÏ´Ï µ¥ÀÌÅÍ°¡ ÃÑ 56ÁÙÀ̶ó¼­, 56¹ø ¹Ýº¹ ÀÛ¾÷ÀÌ µÇ´Â°Å °°³×¿ä. ±×·¡µµ ¿øÇÏ´Â ´ë·Î ¼öÁ¤Àº µÇ¾ú½À´Ï´Ù.)

rmsf_xvg = "rmsf.xvg"
with open(rmsf_xvg, "r") as rmsf_file:
    residue_number = 0
    for data in rmsf_file:
        with open(rmsf_xvg, "r") as rmsf_file:
            column = data.split("  0.")[0]
            residue_number += 1
            read_file = rmsf_file.read()
            edit_file = read_file.replace(str(column), str(residue_number).rjust(5))
            with open(rmsf_xvg, "w") as rmsf_file:
                rmsf_file.write(edit_file)
          
¹è»ó0¿ø 2018-12
read()´Â ÆÄÀÏ Àüü¸¦ ÀнÀ´Ï´Ù. Áö±ÝÀº ÆÄÀÏ Àüü¸¦ Àо ÇÑ °÷À» ¹Ù²Ù°í ´Ù½Ã Àüü¸¦ ±â·ÏÇÏ´Â °ÍÀº ÁÙ ¼ö ¸¸Å­ ¹Ýº¹Çϴ°ÅÁö¿ä.
¿äÁò ÄÄÇ»ÅÍ°¡ ºü¸£´Ï ±×³É ¾²¼Åµµ ±¦ÂúÀ»°Í °°½À´Ï´Ù¸¸...
               
³×ÀÌÃÄ 2018-12
Á¶±Ý¾¿ °øºÎÇϸ鼭 ¼öÁ¤ÇغÁ¾ß°Ú°í, ÀÏ´ÜÀº ±×³É ½á¾ß°Ú³×¿ä ¤¾¤¾
°¨»çÇÕ´Ï´Ù.
timeleaf 2018-12
Àú¶ó¸é Àоî¿Â ÀڷḦ list»óÅ¿¡¼­ ¼öÁ¤ÇÏ°í ¼öÁ¤ÀÌ ³¡³­ ´ÙÀ½ ÇѲ¨¹ø¿¡ ¾²±â ÀÛ¾÷ ÇÒ °Í °°³×¿ä
     
³×ÀÌÃÄ 2018-12
ÆÄÀ̽ã Ãʺ¸¶ó¼­ ¾î¶² ÀǹÌÀÎÁö Àß ¿Í´êÁö°¡ ¾Ê³×¿ä ¤Ð
          
±èÁ¦¿¬ 2018-12
ÁÙÀ» ¹Ýº¹Çϸ鼭  replace Çؼ­ .. ¾î¶² º¯¼ö ¿¡ °è¼Ó Ãß°¡ ½ÃŲµÚ
56ÁÙÀ» ¸ðµÎ Ãß°¡ ½ÃŲµÚ¿¡ write ¸¦ 1¹ø Çϴ°ÅÁÒ ..
          
timeleaf 2018-12
ÆÄÀ̽ãÀ¸·Î
data_list = []
ºó ¸®½ºÆ®¸¦ ¸¸µé°í for¹® ¾È¿¡
data_list.append(¼öÁ¤µÈ ÁÙ string)
ÀÌ°É Ãß°¡ÇØ ÁÖ°í
for¹® ³¡³ª°í ³ª¼­
f = open(''rmsf.xvg", "w")
for data in data_list:
    f.write(data+"\n")
f.close()
ÀÌ·± ½ÄÀ¸·Î¿ä
               
³×ÀÌÃÄ 2018-12
¾Ë·ÁÁֽŰŵµ Çѹø µû¶óÇغ¸°Ú½À´Ï´Ù~ °¨»çÇÕ´Ï´Ù.
±èÁ¦¿¬ 2018-12
±Ùµ¥ with open Àº ¾îµð¼­ º¸¼Å¼­ »ç¿ëÇϽô°ǰ¡¿ä?
¿äÁò ±×³É
f = open('test.txt', mode='wt', encoding='utf-8')


ÀÌ·±½ÄÀ¸·Î ¾²´Â°Å °°Àºµ¥¿ä ..
r = open('test.txt', mode='rt', encoding='utf-8')

¾È¾´Áö ²ÏµÇ¼­ ..
     
³×ÀÌÃÄ 2018-12
°Ë»öÇϸ鼭 ¸»¾¸ÇϽŠ¹æ½Äµµ ºÃ´Âµ¥, ¹º°¡ with¸¦ ¾²´Â°Ô ¸ÚÀÖ¾î? º¸¿©¼­ with¸¦ ½è½À´Ï´Ù.¤¾¤¾
½Å°æ½áÁּż­ °¨»çÇÕ´Ï´Ù~
¹è»ó0¿ø 2018-12
#!/usr/bin/env python
import sys
import os

rmsf_xvg  = "rmsf.xvg"
rmsf_out  = rmsf_xvg + ".out"

fin = open(rmsf_xvg, "r")
fout = open(rmsf_out, "w")

residue_number = 1
for data in fin:
  data_array = data.split()
  fout.write("%5i %8.4f\n"% (residue_number, float(data_array[1])))
  residue_number += 1

fout.close()
fin.close()
     
¹è»ó0¿ø 2018-12
Ȥ½Ã ¿ø ÆÄÀÏÀÌ ¾ÆÁÖ Å¬ °æ¿ì¿¡ ´ëºñÇؼ­ Á¶±Ý ¹Ù²å½À´Ï´Ù.
¾îÁ¦ Áú¹®ÇϽŠ°ªÀÇ Á¤¼öºÎ°¡ 0ÀÌ ¾Æ´Ñ °æ¿ì¿¡µµ Áö¿øÇÕ´Ï´Ù.
          
³×ÀÌÃÄ 2018-12
¿Í Àߵ˴ϴÙ~
Â¥ÁֽŠÄڵ嵵 °øºÎÇغÁ¾ß°Ú½À´Ï´Ù~ °¨»çÇÕ´Ï´Ù.
          
³×ÀÌÃÄ 2018-12
Â¥ÁֽŰŠ¿ä¸®Á¶¸® Çؼ­ ¹Ù²ãºÃ½À´Ï´Ù~~

residue_number = 1

with open("rmsf.xvg", "r") as read_rmsf, open("rmsf_edited.xvg", "w") as write_rmsf:

    for data in read_rmsf:
        column = data.split()[1]
       
        column1 = str(residue_number).rjust(5)
        column2 = str(column).rjust(9)
        write_rmsf.write(column1 + column2 + "\n")
   
        residue_number += 1


QnA
Á¦¸ñPage 997/5686
2014-05   4982895   Á¤ÀºÁØ1
2015-12   1518976   ¹é¸Þ°¡
2015-06   3024   À©µµ¿ì10
2017-06   3024   ±èÁ¦¿¬
2019-01   3024   °£Àå°ÔÀå
2015-07   3024   À嵿°Ç2014
2015-03   3024   ¼ÛÁÖȯ
2019-04   3024   ¸ó½º
2021-01   3024   ¿©ÁÖ³ó¹Î76
2019-05   3024   Àü¼³¼ÓÀǹ̡¦
2019-11   3024   ÄĸÍÁÖ´Ï
2017-05   3025   ´ÃÆĶõ
2023-08   3025   berak
2019-03   3025   ¹Ý¼º¸¸
2017-05   3025   ¿©ÁÖ³ó¹Î76
2020-03   3025   Àü»ê´ã´çÀÚ
2020-05   3025   Á¤ÀǼ®
2020-03   3025   ¸Ó¸®°¨ÀÚ
2021-01   3025   ¿ìÀ¯¸Ô´Â»ç½À
2020-10   3025   ºí·¢ÆÞ
2021-04   3025   ¾Ë¼ö¾ø´ÂÈû
2020-09   3025   À±ÇϹÀÀ