Skip to content

Instantly share code, notes, and snippets.

@su27
Last active January 21, 2019 03:51
Show Gist options
  • Save su27/a3bd23f82ed85e9c07f5e89912f4296c to your computer and use it in GitHub Desktop.
Save su27/a3bd23f82ed85e9c07f5e89912f4296c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
def main():
f = open('game_log.tsv', encoding='utf-8')
wf = open('game_log_new.csv', 'w', encoding='utf-8')
for i, l in enumerate(f.readlines()):
if i == 0:
wf.write('event_time,event_lat,event_lng,tracker_trigger,comments\n')
else:
l = l.replace('None', '')
data = l.strip().split('\t')
data[0] = data[0].replace(' ', '-', 1)
wf.write('%s\n' % ','.join(data))
wf.close()
f.close()
if __name__ == '__main__':
main()
@su27
Copy link
Author

su27 commented Jan 9, 2019

下载这个脚本,放在解压后的 game_log.tsv 同一目录下,然后执行:

python modify.py

会生成一个新文件 game_log_new.csv

@LeoQuote
Copy link

LeoQuote commented Jan 12, 2019

wf.close 完了之后是不是 f.close() 也应该有?

@LeoQuote
Copy link

windows 下有默认编码的问题, 我加了编码:

#!/usr/bin/env python


def main():
    f = open('game_log.tsv',encoding='utf-8')
    wf = open('game_log_new.csv', 'w', encoding='utf-8')
    for i, l in enumerate(f.readlines()):
        if i == 0:
            wf.write('event_time,event_lat,event_lng,tracker_trigger,comments\n')
        else:
            l = l.replace('None', '')
            data = l.strip().split('\t')
            data[0] = data[0].replace(' ', '-', 1)
            wf.write('%s\n' % ','.join(data))
    wf.close()
    f.close()


if __name__ == '__main__':
    main()

感谢大爷的反馈.

@su27
Copy link
Author

su27 commented Jan 21, 2019

@LeoQuote 竟然有编码问题,没试Windows,捂脸。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment