十一月 05
tchinese.py
#!/bin/python
print "Python是個酷玩意"
python tchinese.py
SyntaxError: Non-ASCII character '\xe6' in file tchinese.py on line 2, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
錯誤訊息中的連結到這篇文章:PEP 0263 — Defining Python Source Code Encodings
文中提到:
To define a source code encoding, a magic comment must be placed into the source files either as first or second line in the file, such as:
or (using formats recognized by popular editors)
#!/usr/bin/python
# -*- coding: <encoding name> -*-
or
#!/usr/bin/python
# vim: set fileencoding=<encoding name> :
Example:
#!/bin/python
# -*- coding: utf-8 -*-
print "Python是個酷玩意"
Result:
Tagged with: CJKV • Programming • Python • tips • UTF-8
十一月 02
PHP 檔案處理
讀檔:
1.fopen
範例:
<?php
$handle = fopen("open_file.txt", "r");
if( $handle === FALSE)
{
echo "Open File Fail" . "<br>";
}
else
{
echo "Open File Success" . "<br>";
while (!feof($handle))
{
$current_line = fgets($handle);
echo $current_line . "<br>";
}
}
?>
2.file_get_contents
範例:
<?php
$handle = file_get_contents('open_file.txt');
echo $handle;
?>
=========================================================
寫檔:
1.fwrite
<?php
$handle = fopen('open_file.txt', 'w');
fwrite($handle, "test1");
fclose($fp);
?>
ps:fwrite 是沒有加換行字元,如果要換行必須加上 \n 處理
2.file_put_contents
<?php
$handle = 'open_file.txt';
$current_line = "line1\n";
file_put_contents($handle, $current_line);
?>
source: PHP:檔案處理 @ 拉不拉多的夢幻世界 :: 痞客邦 PIXNET ::
Tagged with: PHP • Programming • tips
最新回應